[removed] True form reset for hidden fields

前端 未结 10 1652
臣服心动
臣服心动 2020-12-14 17:12

Unfortunately form.reset() function doesn\'t reset hidden inputs of the form. Checked in FF3 and Chromium.

Does any one have an idea how to do the reset for hidden f

10条回答
  •  鱼传尺愫
    2020-12-14 17:22

    I found it easier to just set a default value when the document is loaded then trap the reset and reset the hidden puppies back to their original value. For example,

    //fix form reset (hidden fields don't get reset - this will fix that pain in the arse issue)
    $( document ).ready(function() {
      $("#myForm").find("input:hidden").each(function() {
          $(this).data("myDefaultValue", $(this).val());
      });
    
      $("#myForm").off("reset.myarse");
      $("#myForm").on("reset.myarse", function() {
         var myDefaultValue = $(this).data("myDefaultValue");
         if (myDefaultValue != null) {
           $(this).val(myDefaultValue);
         }
      });
    }
    

    Hope this helps someone out :)

提交回复
热议问题