[英]javascript require a checkbox to be checked from an array of checkboxes
I currenlty have some javascript/jquery code that displays an error message if no boxes are checked. 我目前有一些javascript / jquery代码,如果未选中任何框,则会显示错误消息。 However when I check a box and submit the form the error message still displays.
但是,当我选中一个框并提交表单时,仍然会显示错误消息。
any help would be great! 任何帮助将是巨大的!
javascript code : JavaScript代码 :
var hobbies = $('#hobbies').val();
if ($('#hobbies :checkbox:checked').length === 0){
$("#multichk").after('<span class="error">Please choose at least one.</span>');
hasError = true;
}
html code: html代码:
<input type='checkbox' id='hobbies' name='hobbies[0]' value='1'/><label for='hobbies0'>football</label><br />
<input type='checkbox' id='hobbies' name='hobbies[1]' value='2'/><label for='hobbies1'>soccer</label><br />
<input type='checkbox' id='hobbies' name='hobbies[2]' value='3'/><label for='hobbies2'>baseball</label><br />
<input type='checkbox' id='hobbies' name='hobbies[3]' value='4'/><label for='hobbies3'>tennis</label><br />
<input type='hidden' id='multichk' />
Thank you. 谢谢。
The way you have the checkboxes named and identified, they won't be part of the same group, and they won't be properly identified. 复选框具有命名和标识的方式,它们不会属于同一组,也无法正确标识。
Each element on a page, when identified, must have a unique ID, and for a group of checkboxes, or radio buttons for that matter, to be validated and delivered as a group, each member of the group must have the same name. 页面上的每个元素在被标识时都必须具有唯一的ID,并且对于要作为一个组进行验证和交付的一组复选框或单选按钮,该组的每个成员必须具有相同的名称。
Given these rules, it should come as no surprised that the for
attribute of a label tag references an element's id
attribute, rather than its name
attribute. 有了这些规则,标签标记的
for
属性引用元素的id
属性而不是其name
属性就不足为奇了。
Now, I'm guessing that you're using PHP for your server-side language (based on the brackets in the checkbox names), in which case the checkboxes should be declared like so: 现在,我猜想您正在使用PHP作为服务器端语言(基于复选框名称中的括号),在这种情况下,应该这样声明复选框:
<input type='checkbox' id='hobbies0' name='hobbies[]' value='1'/>
<label for='hobbies0'>football</label><br />
<input type='checkbox' id='hobbies1' name='hobbies[]' value='2'/>
<label for='hobbies1'>soccer</label><br />
<input type='checkbox' id='hobbies2' name='hobbies[]' value='3'/>
<label for='hobbies2'>baseball</label><br />
<input type='checkbox' id='hobbies3' name='hobbies[]' value='4'/>
<label for='hobbies3'>tennis</label><br />
[Note: labels only moved to next line to remove horizontal scroll in code box] [注意:标签仅移至下一行以删除代码框中的水平滚动]
With the HTML straightened out, the jQuery would look something like this: 整理好HTML之后,jQuery看起来像这样:
if($('input:checkbox[name="hobbies[]"]:checked').length === 0){
$("#multichk").after('<span class="error">Please choose at least one.</span>');
hasError = true;
}
Reference for selector format 选择器格式参考
If that selector still gives you trouble, then you may need to prefix the square brackets with double backslashes like this: 如果该选择器仍然给您带来麻烦,则可能需要在方括号前面加上双反斜杠,如下所示:
if($('input:checkbox[name="hobbies\\[\\]"]:checked').length === 0){
Reference for double backslashes 双反斜杠参考
Hope this helps 希望这可以帮助
Technically your HTML is invalid; 从技术上讲,您的HTML无效; multiple elements with the same id is illegal.
具有相同ID的多个元素是非法的。 You should use a class selector instead of an id selector...
您应该使用类选择器而不是ID选择器...
Anyway, the easiest way is to put the span there and clear it when someone checks a check box. 无论如何,最简单的方法是将跨度放置在此处并在有人选中复选框时将其清除。
<input type='checkbox' id='hobbies' name='hobbies[0]' value='1'/><label for='hobbies0'>football</label><br />
<input type='checkbox' id='hobbies' name='hobbies[1]' value='2'/><label for='hobbies1'>soccer</label><br />
<input type='checkbox' id='hobbies' name='hobbies[2]' value='3'/><label for='hobbies2'>baseball</label><br />
<input type='checkbox' id='hobbies' name='hobbies[3]' value='4'/><label for='hobbies3'>tennis</label><br />
<input type='hidden' id='multichk' />
<span id="error_message" class="error"></span>
Then your js: 然后你的js:
var hobbies = $('#hobbies').val();
if ($('#hobbies:checked').length === 0) {
$('#error_message').innerHTML('Please choose at least one.');
hasError = true;
} else {
$('#error_message').innerHTML('');
hasError = false;
}
You can reset it whenever they click a checkbox by setting up a handler: 只要他们单击复选框,就可以通过设置处理程序来重置它:
function changed(checkbox) {
return function() {
if (checkbox.checked) {
$('#error_message').innerHTML('');
hasError = false;
}
}
}
var checkboxes = document.querySelector('#hobbies')
for (var i=0; i<checkboxes.length; ++i) {
checkboxes[i].onchange = changed(checkboxes[i]);
Above is the idea; 以上是想法; working code in this fiddle .
提琴的工作代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.