Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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
Javascript 使用jQuery在组中选择多个复选框_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

Javascript 使用jQuery在组中选择多个复选框

Javascript 使用jQuery在组中选择多个复选框,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我必须在一个组中选择多个复选框。如果我必须按用户提供类似[“1”、“3”、“8”]的列表,现在有10个复选框的值属性设置为0到9,并且我必须选择值为1、3和8的复选框。如何容纳??请回复 举例来说,对于值8: <input type="checkbox" class="chk" name="myGroup[]" value="7" /> <input type="checkbox" class="chk" name="myGroup[]" value="8" /> &l

我必须在一个组中选择多个复选框。如果我必须按用户提供类似[“1”、“3”、“8”]的列表,现在有10个复选框的值属性设置为0到9,并且我必须选择值为1、3和8的复选框。如何容纳??请回复

举例来说,对于值8:

<input type="checkbox" class="chk" name="myGroup[]" value="7" />
<input type="checkbox" class="chk" name="myGroup[]" value="8" />

<script>
  $(".chk[value='8']").prop("checked", true);
</script>

$(“.chk[value='8']”)prop(“选中”,为真);

举例说明,对于值8:

<input type="checkbox" class="chk" name="myGroup[]" value="7" />
<input type="checkbox" class="chk" name="myGroup[]" value="8" />

<script>
  $(".chk[value='8']").prop("checked", true);
</script>

$(“.chk[value='8']”)prop(“选中”,为真);
您可以试试这个-

var arr =  ["1","3","8"];
$(':checkbox').each(function(){
  if($.inArray(this.value,arr) > -1){
   $(this).prop('checked',true);
  }
}) 
你可以试试这个-

var arr =  ["1","3","8"];
$(':checkbox').each(function(){
  if($.inArray(this.value,arr) > -1){
   $(this).prop('checked',true);
  }
}) 

这是一种方法——给你的复选框一个公共类,即“myCheckbox”

使用jQuery遍历类为“myCheckbox”的所有复选框,并将它们的值与用户提供的列表进行比较。如果存在匹配项,请选中该复选框

伪jQuery代码:

$('.myCheckbox').each(function() {
  // you would search the array here
  if ($(this).value == "1") {
    $(this).check();
  }
});

这是一种方法——给你的复选框一个公共类,即“myCheckbox”

使用jQuery遍历类为“myCheckbox”的所有复选框,并将它们的值与用户提供的列表进行比较。如果存在匹配项,请选中该复选框

伪jQuery代码:

$('.myCheckbox').each(function() {
  // you would search the array here
  if ($(this).value == "1") {
    $(this).check();
  }
});

或者,使用
过滤器()

但是,在设置属性之前,可能需要先取消选中任何已选中的复选框:

var arr = ['1','3','8'];

$('input[type=checkbox]').prop('checked',false).filter(function(){
    return $.inArray(this.value, arr) > -1;
}).prop('checked',true);

最后,因为
输入
始终是一个字符串,而JavaScript中的数字不必加引号,以允许数组中出现未加引号的数字(防止遗忘,如果没有其他内容):

参考资料:

或者,使用
过滤器()

但是,在设置属性之前,可能需要先取消选中任何已选中的复选框:

var arr = ['1','3','8'];

$('input[type=checkbox]').prop('checked',false).filter(function(){
    return $.inArray(this.value, arr) > -1;
}).prop('checked',true);

最后,因为
输入
始终是一个字符串,而JavaScript中的数字不必加引号,以允许数组中出现未加引号的数字(防止遗忘,如果没有其他内容):

参考资料:


这将解决您的问题:

var setArr = ["1","3","8"];

$('.checkbox').each(function(i, j){
if(jQuery.inArray($(j).val(), setArr)!= -1){

$(j).attr('checked', 'checked');
}

});

这将解决您的问题:

var setArr = ["1","3","8"];

$('.checkbox').each(function(i, j){
if(jQuery.inArray($(j).val(), setArr)!= -1){

$(j).attr('checked', 'checked');
}

});

您要查找用户选中了哪些复选框,或者您必须根据从某处收到的数组选中特定复选框?您要查找用户选中了哪些复选框,或者您必须根据从某处收到的数组选中特定复选框?