[removed] True form reset for hidden fields

前端 未结 10 1630
臣服心动
臣服心动 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 :)

    0 讨论(0)
  • 2020-12-14 17:24

    You can use jQuery - this will empty hidden fields:

    $('form').on('reset', function() {
      $("input[type='hidden']", $(this)).val('');
    });
    

    Tip: just make sure you're not resetting csrf token field or anything else that shouldn't be emptied. You can narrow down element's specification if needed.

    If you want to reset the field to a default value you can use(not tested):

    $('form').on('reset', function() {
      $("input[type='hidden']", $(this)).each(function() {
        var $t = $(this);
        $t.val($t.data('defaultvalue'));
      });
    });
    

    and save the default value in the data-defaultvalue="Something" property.

    0 讨论(0)
  • 2020-12-14 17:26

    How I would do it is put an event listener on the change event of the hidden field. In that listener function you could save the initial value to the DOM element storage (mootools, jquery) and then listen to the reset event of the form to restore the initial values stored in the hidden form field storage.

    0 讨论(0)
  • 2020-12-14 17:33

    Create a button and add JavaScript to the onClick event which clears the fields.

    That said, I'm curious why you want to reset these fields. Usually, they contain internal data. If I would clear them in my code, the post of the form would fail (for example after the user has entered the new data and tries to submit the form).

    [EDIT] I misunderstood your question. If you're worried that someone might tamper with the values in the hidden fields, then there is no way to reset them. For example, you can call reset() on the form but not on a field in the form.

    You could think that you could save the values in a JavaScript file and use that to reset the values but when a user can tamper with the hidden fields, he can tamper with the JavaScript as well.

    So from a security point of view, if you need to reset hidden fields, then avoid them in the first place and save the information in the session on the server.

    0 讨论(0)
  • 2020-12-14 17:34

    You can reset hidden input field value using below line, you just need to change your form id instead of frmForm.

    $("#frmForm input:hidden").val(' ');
    
    0 讨论(0)
  • 2020-12-14 17:39

    Seems the easiest way of doing that is having display: none text field instead of hidden field. At this case default reset process regularly.

    0 讨论(0)
提交回复
热议问题