[removed] True form reset for hidden fields

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

提交回复
热议问题