作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下复选框列表,它是从服务器以 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);
关于javascript - 通过比较值来选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6186582/
我是一名优秀的程序员,十分优秀!