Why does the reset button on html forms not reset hidden fields?

前端 未结 4 1024
醉梦人生
醉梦人生 2021-01-04 06:07

I discovered something surprising:





        
相关标签:
4条回答
  • 2021-01-04 06:42

    FWIW, I think I can put together the full story from the answers and comments.

    Usage rationale: The clear button is intended for clearing user input, and since hidden inputs are not directly accessible by the user, it doesn't make sense to allow the user to reset the hidden input's value.

    Documentation and behavior: The bug report that AR pointed out is explicit about what is happening: The hidden field's value's mode is default, as is intended in the specs.
    Particularly, this means that changing the value (as in the sample code in the question) changes the default value, and the reset button resets input fields to the default value, hence there is no change.
    The behavior for the text input is different (even though its value is also changed programmatically) because its value's mode is not default but value, which means that there is a distinction between the default value of the input and the current value.

    0 讨论(0)
  • 2021-01-04 06:44

    The user can't see or modify the hidden field, therefore it wouldn't make any sense for them to be able to clear it by pressing a button.

    0 讨论(0)
  • 2021-01-04 06:48

    I came here because I bumped into this same issue. However, upon further reflection it is (usually) a very desired behavior.

    The hidden field is most commonly used for storing information that was sent by the server when the page loaded.

    Although, it can also be used by the script to enter some runtime-calculated entries, resetting hidden fields can cause trouble when not intended.

    0 讨论(0)
  • 2021-01-04 07:02

    When clicking on reset, the browser goes and check the default value in the DOM tree, not in the HTML page.

    Your Javascript modifies the DOM. You should try to replace your RESET button with a custom button that calls a Javascript that does two things:

    • reset normally.
    • set the hidden field to 5.
    0 讨论(0)
提交回复
热议问题