Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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中控制多个(通过php和db生成)复选框?_Jquery_Checkbox_Mousedown - Fatal编程技术网

如何在jquery中控制多个(通过php和db生成)复选框?

如何在jquery中控制多个(通过php和db生成)复选框?,jquery,checkbox,mousedown,Jquery,Checkbox,Mousedown,我的html页面中有一个复选框列表 <input type="checkbox" id="example1" value="1"> <input type="checkbox" id="example2" value="2"> 但这种方式意味着知道我不知道的复选框的名称/id,因为它是生成的。。。 那么,如何通过jquery生成我的复选框和控件?尝试为复选框设置一个类: <input type="checkbox" class="cls1" id="examp

我的html页面中有一个复选框列表

 <input type="checkbox" id="example1" value="1">
 <input type="checkbox" id="example2" value="2">
但这种方式意味着知道我不知道的复选框的名称/id,因为它是生成的。。。
那么,如何通过jquery生成我的复选框和控件?

尝试为复选框设置一个类:

<input type="checkbox" class="cls1" id="example1" value="1">
<input type="checkbox" class="cls1" id="example2" value="2">

这是为了尽可能地远离您的原始代码,这可能不是最好的。如果没有更广阔的视野,很难说。请注意,在动态添加clientside复选框的情况下(我假设不是这种情况,因为您讨论的是使用PhP生成它)你需要一个不同的选择器才能使用所谓的

你可以在复选框控件上循环,而不是用ID点击它们。或者给它们一个类,然后用该类选择所有元素…给它们一个类而不是ID似乎是可行的,但我如何获得hitten复选框的值?cfr我的答案。。。不要看我关于循环浏览项目的评论,我不知道你想要保留“选中”事件,并且想要在提交表单时处理所有复选框。工作就像一个符咒!非常感谢。
<input type="checkbox" class="cls1" id="example1" value="1">
<input type="checkbox" class="cls1" id="example2" value="2">
 $('.cls1').mousedown(function() {   
    if (!$(this).is(':checked')) {
        this.checked = confirm("Are you sure?");
        $(this).trigger("change");
    }
 });