Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Jquery选中复选框_Jquery_Html_Css - Fatal编程技术网

使用Jquery选中复选框

使用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=

根据我的要求,在表单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="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);
});