我有一个复选框列表,我正在尝试根据文本框的值检查这些复选框。
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