Force a checkbox to always submit, even when unchecked

后端 未结 7 1358
粉色の甜心
粉色の甜心 2020-12-01 23:36

I have an html form and i would like ALWAYS to have checkboxes to submit a value. How can i do that? I have one idea but i havent tried it and i am unsure if its the best wa

7条回答
  •  悲哀的现实
    2020-12-02 00:25

    HTML doesn't work that way. HTML checkboxes are specified as follows: if checked, then its name=value will be sent as request parameter. If unchecked, then its name=value will not be sent as request parameter. Note that when the value is unspecified, then most browsers default to "on". It's easier if you give all checkboxes the same name but a different and fixed value. This way you can obtain the checked ones as an array/collection.

    If all checkboxes are already known beforehand in server side, you can just apply basic math to obtain the unchecked checkboxes:

    uncheckedCheckboxes = allCheckboxes - checkedCheckboxes
    

    If those checkboxes are created dynamically at the client side and therefore unknown beforehand in server side, then add for each checkbox a field containing information about the dynamically created checkbox, so that the server side knows which checkboxes are all present as the moment of submission.

提交回复
热议问题