我有以下内容:
116 $.each(data.entries, function(id, item) {
117 console.log($("input[type=checkbox][value=" + item + "]").is(":checked"));
118 $("input[type=checkbox][value=" + item + "]").prop("checked", true);
119 console.log($("input[type=checkbox][value=" + item + "]").is(":checked"));
120 })
对于我的生活,我无法检查我的复选框:
swot-nav.js:117 false
swot-nav.js:119 false
这也不起作用:
116 $.each(data.entries, function(id, item) {
117 console.log($(":checkbox[value=" + item + "]").is(":checked"));
118 $(":checkbox[value=" + item + "]").prop("checked", "true");
119 console.log($(":checkbox[value=" + item + "]").is(":checked"));
120 })
(同样的结果)
我能错过什么?
修改:
这是在Ajax Call的成功函数中发生的。
这就是数据的样子:
data = {'label': 'D1- DC Test 1', 'details': '', 'type': 'D', 'entries': [20, 21, 22]
这是一个有效的小提琴:https://jsfiddle.net/e3arwp7q/
我的代码中必须存在更严重的错误。如果这看起来像是应该工作的每个人,那么也许我应该寻找其他地方。
答案 0 :(得分:0)
The problem is that you are passing the wrong argument to the is
method. Use .is(':checked')
instead.
答案 1 :(得分:0)
Ok I think you need to loop through your checkbox
s .. the next code will loop through just the unchecked
inputs
$.each(data.entries, function(id, item) {
$('input[type=checkbox]:not(:checked)').each(function(){
$(this).filter('[value=' + item + ']').prop("checked", true);
});
});
Note: The above code will work if 1- output of
item
matching the value ofcheckbox
2- if you have more than onecheckbox
with the same value
The next code will work for all checkbox
inputs
$.each(data.entries, function(id, item) {
$('input[type=checkbox]').each(function(){
var checked_or_not = this.checked;
console.log(checked_or_not);
if(checked_or_not === false){
$(this).filter('[value=' + item + ']').prop("checked", true);
}
console.log(checked_or_not);
});
});
答案 2 :(得分:0)
you should use :checked selector.
add colon before checked
change is("checked") to is(":checked")