How to submit unchecked checkbox also

后端 未结 6 1299
滥情空心
滥情空心 2020-12-17 16:56

If checkbox is not checked, clicking on form submit button does not submit any data.

Some checkboxes may be not present in form depending on form fields selected by

6条回答
  •  没有蜡笔的小新
    2020-12-17 17:16

    My shortest solution: use a checkbox's onchange() event to set a hidden sibling to 0 or 1. As follows:

    var htm = '0?"checked":"")+' onchange="this.nextSibling.value=this.checked==true?1:0;" />';
    

    Submit ONLY sends the hidden sibling, NOT the checkbox because it has no name attribute. So the above is serialized as "...&Gender=1..." or "...&Gender=0..." depending on isMale value.

提交回复
热议问题