如何解决通过比较值来使复选框处于选中状态
|| 我有以下复选框列表,该列表是从服务器以HTML格式生成的。我也有一个如下的Javascript数组。 我想遍历数组,并针对每个复选框检查每个值。如果有匹配项,请选中该复选框。 我正在使用jQuery 1.4。<script>
//this array is a result of an Ajax call
var arrMonth = [ \"November\",\"October\",\"December\"];
//Loop through the array and check each element against the check box list
//if found,make the checkbox checked
jQuery.each(arrMonth,function( intIndex,objValue) {
// script to check against the each checbox in the table ctl00_ContentPlaceHolder1_cbStudentTypeEdit
});
</script>
生成的复选框列表:
<table cellspacing=\"10\" cellpadding=\"20\" border=\"0\" id=
\"ctl00_ContentPlaceHolder1_cbStudentTypeEdit\">
<tbody>
<tr>
<td><span idkey=\"1\"><input type=\"checkbox\" tabindex=\"3\" name=
\"ctl00$ContentPlaceHolder1$cbStudentTypeEdit$0\" id=
\"ctl00_ContentPlaceHolder1_cbStudentTypeEdit_0\" />
<label for=\"ctl00_ContentPlaceHolder1_cbStudentTypeEdit_0\">October</label></span></td>
<td><span idkey=\"3\"><input type=\"checkbox\" tabindex=\"3\" name=
\"ctl00$ContentPlaceHolder1$cbStudentTypeEdit$2\" id=
\"ctl00_ContentPlaceHolder1_cbStudentTypeEdit_2\" />
<label for=\"ctl00_ContentPlaceHolder1_cbStudentTypeEdit_2\">November</label></span></td>
</tr>
<tr>
<td><span idkey=\"2\"><input type=\"checkbox\" tabindex=\"3\" name=
\"ctl00$ContentPlaceHolder1$cbStudentTypeEdit$1\" id=
\"ctl00_ContentPlaceHolder1_cbStudentTypeEdit_1\" />
<label for=\"ctl00_ContentPlaceHolder1_cbStudentTypeEdit_1\">December</label></span></td>
<td><span idkey=\"4\"><input type=\"checkbox\" tabindex=\"3\" name=
\"ctl00$ContentPlaceHolder1$cbStudentTypeEdit$3\" id=
\"ctl00_ContentPlaceHolder1_cbStudentTypeEdit_3\" />
<label for=\"ctl00_ContentPlaceHolder1_cbStudentTypeEdit_3\">January</label></span></td>
</tr>
</tbody>
</table>
谢谢。
解决方法
// jQuery wrapper
(function($){
// document-ready wrapper
$(function(){
var arrMonth = [ \"November\",\"October\",\"December\"];
//Loop through the array and check each element against the check box list
//if found,make the checkbox checked
$.each(arrMonth,function( intIndex,objValue) {
$(\"label:contains(\'\" + objValue + \"\')\").each(function(){
// jquery 1.5 and earlier: use attr instead of prop
$(\"#\" + $(this).attr(\"for\")).prop(\"checked\",true);
});
});
});
})(jQuery);
工作演示:http://jsfiddle.net/roberkules/3QjYk/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。