使用Jquery选中复选框
根据我的要求,在表单5中执行复选框,如使用Jquery选中复选框,jquery,html,css,Jquery,Html,Css,根据我的要求,在表单5中执行复选框,如 <form action="#" method="post"> <input type="checkbox" class="ch-1" /> /* Need to select */ <input type="checkbox" class="ch-2" /> /* Need to select */ <input type="checkbox" class=
<form action="#" method="post">
<input type="checkbox" class="ch-1" /> /* Need to select */
<input type="checkbox" class="ch-2" /> /* Need to select */
<input type="checkbox" class="bx-3" />
<input type="checkbox" class="bx-4" />
<input type="checkbox" class="ch-5" /> /* Need to select */
<input type="submit" value="Select" />
</form>
现在,我需要选中那些起始类名为ch的复选框,然后单击使用jquery提交。您可以使用它来定位以类开头的元素。要选择它们,请将property checked设置为true:
$('[class^="ch-"]').prop('checked',true);
尝试使用:
$("input[class^=ch-]").prop("checked",true);
您可以使用属性选择器来获取具有类ch的复选框-
$('form').submit(function(){
var chkboxes = $('[class^=ch-]');
});
如果只希望选中复选框,则可以使用:选中选择器
var chkboxes = $('[class^=ch-]:checked');
使用jQuery选择器
$('#submit').click(function){
$(':checkbox[class^="ch-"]').prop('checked',true);
})
要获得复选框,请使用:checked
尝试以下操作:在表单的提交事件上,使用jQuery选择类名从“ch-”开始的所有复选框
$('form').submit(function(){
$('[class^="ch-"]').prop('checked',true);
});
为什么在提交表单时要勾选这些复选框?相反,您可以直接在HTML标记中选中这些复选框。@SatishUpadhyay:很高兴这有帮助。请投票并接受有帮助的答案。
$('form').submit(function(){
$('[class^="ch-"]').prop('checked',true);
});