1

我有一个复选框列表,我正在尝试根据文本框的值检查这些复选框。

var codes = $('#textbox1').val().split(','); // this contains [ "Dept1" and "Dept2" ]
var allDeptChecks = $('.Dept-help input:checkbox'); // this contains all of my checkboxes
allDeptChecks.attr('checked', false);
$.each(codes, function() {
    var chkDept = allDeptChecks.find("[value='"+this+"']"); // no matter what I try, this selector matches 0 elements
    chkDept.attr('checked', true);
}); 

<li title="Dept 1">
    <input type="checkbox" name="chkDept1" value="Dept1">
    <span>Dept1</span>
    <span>Description Numero Uno</span>
</li>
<li title="Dept 2">
    <input type="checkbox" name="chkDept2" value="Dept2">
    <span>Dept2</span>
    <span>Description Numero Dos</span>
</li>

这不应该工作吗?

编辑: 这是一个 JSFiddle

4

2 回答 2

1

干得好

$('#lnkGo').click(function () {
    var codes = $('#textbox1').val().split(','); // this contains [ "Dept1" and "Dept2" ]
    var allDeptChecks = $('input:checkbox'); // this contains all of my checkboxes
    allDeptChecks.prop('checked', false);
    $.each(codes, function () {
        var chkDept = allDeptChecks.filter("input[value='" + this + "']"); // no matter what I try, this selector matches 0 elements
        chkDept.prop('checked', true);
    });
});
于 2013-01-24T13:39:57.597 回答
1

试试这个 :

$.each(codes, function(i, value) {
$("input[value='"+value+"']").prop('checked', true);    
});  
于 2013-01-24T13:43:00.273 回答