Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.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中选中值为specific list的复选框的最快方法_Javascript_Jquery_Json_Asp.net Mvc 3 - Fatal编程技术网

Javascript 在jQuery中选中值为specific list的复选框的最快方法

Javascript 在jQuery中选中值为specific list的复选框的最快方法,javascript,jquery,json,asp.net-mvc-3,Javascript,Jquery,Json,Asp.net Mvc 3,这是我的HTML Dom: <input type="checkbox" class="chk-act" name="actionChk" value="17"> <input type="checkbox" class="chk-act" name="actionChk" value="18"> <input type="checkbox" class="chk-act" name="actionChk" value="19"> <input type

这是我的HTML Dom:

<input type="checkbox" class="chk-act" name="actionChk" value="17">
<input type="checkbox" class="chk-act" name="actionChk" value="18">
<input type="checkbox" class="chk-act" name="actionChk" value="19">
<input type="checkbox" class="chk-act" name="actionChk" value="20">
<input type="checkbox" class="chk-act" name="actionChk" value="21">

该列表最多可以有200个成员。所以我想找出返回响应时,最快的方法来选中上面列表中有值的复选框。您的建议是什么?

最快的方法是在复选框列表中添加id标识符。例如—

<input type="checkbox" class="chk-act" name="actionChk" value="17" id='chk-act-17' />
//incidentally, I hope the lack of a closing '/' was an oversight
//when you get your JSON response, in your ajax success or done methods
var resp = ["12", "232"],
    respLength = resp.length;
for(var i = 0; i < respLength; i += 1){
    $('#chk-act-' + resp[i]).prop('checked', true);
    //if your are using jquery 1.7+ that is, otherwise use, 
    $('#chk-act-' + resp[i]).attr('checked', 'checked');
}
在此基础上混合jQuery

arr = ["18","20","21"];
$("input:checkbox").prop('checked', false); //If you need to uncheck other boxes too
$.each(arr, function(k,v) {       
   $("input[value=\""+v+"\"]").prop('checked', true);
});
|您可以选中以下复选框:

试试这个:

 arr = ["18","20","21"];

for(var i=0; i<arr.length; i++)
 {
    if(!$("input[value="+arr[i]+"]").is(':checked'))
    { 
        $("input[value="+arr[i]+"]").attr('checked', 'checked');
    }
 }
arr=[“18”、“20”、“21”];

for(var i=0;i

仅在XML中需要(因此XHTML)但是没有HTML版本。@powerbuoy我错了,XHTML它不知道jsperf.com…太好了!稍微更正一下。您需要取消选中其余的复选框。@Starx这不起作用,因为它将在上一次迭代中取消选中复选框。例如,对于18,它将选中18,对于20,它将取消选中18,然后选中20仅限。同样,在结束时,这将只检查最后一次value@Jayanga,哦,是不是犯了一个错误TheAttr checked不起作用…它在某个地方出现了一些问题,再次出现了同样的疑问,如果我们想取消选中其余的复选框,那么考虑到数组正在动态生成,您可以尝试
.attr('checked',true)
不会导致问题您可以选中fiddle,即数组将被更改,也可以选中事件发生更改复选框。m仍然与您的要求相混淆……这不是真的……对于jquery 1.6.4(或其他类似内容),dom元素使用
checked=“true”
未标识为
:选中的
-请注意!您的代码非常好,但只供一次使用。如果我想在不刷新html的情况下再次使用它,该怎么办?假设某些复选框已被选中。那么操作步骤是什么?
arr = ["18","20","21"];
$("input:checkbox").prop('checked', false); //If you need to uncheck other boxes too
$.each(arr, function(k,v) {       
   $("input[value=\""+v+"\"]").prop('checked', true);
});
 arr = ["18","20","21"];

for(var i=0; i<arr.length; i++)
 {
    if(!$("input[value="+arr[i]+"]").is(':checked'))
    { 
        $("input[value="+arr[i]+"]").attr('checked', 'checked');
    }
 }