如果选中一个复选框,我想检查所有其他复选框。 我的代码
检查名为report的复选框。我想检查所有带有名称报告的复选框。
有人可以帮我这个吗? 谢谢
用这个:
$(function(){ // add multiple select / deselect functionality $("#selectall").click(function () { $('.case').attr('checked', this.checked); }); });
其中#selectall
是您的“报告”复选框的ID,而.case
是您希望在单击#selectall
选择的所有chekbox的cssclass
对于较新版本的jquery,您也可以使用prop
函数而不是attr
好:
$('input[name="report"]').click(function(){ $('input[name="reports"]').prop("checked", this.checked); });
单击“报告”复选框后,所有“报告”复选框将设置为与“报告”相同的状态。 如果您希望它只能单向工作,即,如果取消选中“report”复选框,则不应取消选中其他复选框,请执行以下操作:
$('input[name="report"]').click(function(){ if (this.checked) $('input[name="reports"]').prop("checked", true); });
上面应该放在document.ready处理程序和/或在所有复选框之后出现的脚本块中。
如果您使用的是旧版本的jQuery,则需要使用.attr()
而不是.prop()
。
简单演示(还展示了如何使用document.ready): http : //jsfiddle.net/eHLcS/
试试这个:
$('input[name="report"]').change(function() { $('input[name="reports"]').attr("checked", $(this).is(":checked")); });
在所有复选框的onclick事件(姓名==’报告’)中附加以下function
function CheckAllReportCheckboxes() { var elm = document.getElementsByTagName("input"); for (i = 0; i < elm.length; i++) { if (elm[i].type == "checkbox" && elm[i].name == 'report') { elm[i].checked = 'checked'; } } }
请记住,取消选中复选框将无法正常工作。
以上就是jQuery教程分享选中一个复选框可选中所有复选框相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/558677.html