强制复选框始终提交,即使未选中 [英] Force a checkbox to always submit, even when unchecked

查看:116
本文介绍了强制复选框始终提交,即使未选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html表单,我希望始终有复选框提交一个值。我怎样才能做到这一点?我有一个想法,但我没有尝试过,我不确定它是否是最好的方法来做到这一点(jquery检查框是否被选中,然后将值设置为0/1并检查它,以便它会提交)

解决方案

这完全违背了HTML中复选框的自然/指定行为。如果选中,则它的将作为参数发送。如果未选中,则它的不会作为参数发送。你还可以在服务器端如何区分选中的复选框和未选中的复选框?请注意,当未指定时,大多数浏览器默认为on。如果您给所有复选框使用相同的名称,但不同的和固定的,则更容易。如果所有的复选框事先已经在服务器端知道了,那么你可以应用基本的数学运算来获得未经检查的数据集合。复选框:

  uncheckedCheckboxes = allCheckboxes  -  checkedCheckboxes 

这也是正常的做法。如果您更多地了解用于处理表单的服务器端语言,我们将能够提供更多提示/技巧,以便如何以最佳方式实现此目的。



如果这些复选框是在客户端动态创建的,则为每个复选框添加一个< input type =hidden> 字段,其中包含有关复选框的信息,以便服务器端知道哪些复选框都是提交时刻。


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 way to do it (jquery to check if the box is checked or not, then set the value to 0/1 and check it off so it will submit)

解决方案

This goes totally against the natural/specified behaviour of checkboxes in HTML. If checked, then its value will be sent as parameter. If unchecked, then its value will not be sent as parameter. How else would you in the server side be able to distinguish checked from unchecked checkboxes? 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

That's also the normal practice. If you tell a bit more about the server side language you're using to process the form, we would be able to give more tips/tricks how to achieve this the best way.

If those checkboxes are created dynamically at the client side, then add for each checkbox a <input type="hidden"> field containing information about the checkbox, so that the server side knows which checkboxes are all present as the moment of submission.

这篇关于强制复选框始终提交,即使未选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆