gpt4 book ai didi

javascript - 通过比较值来选中复选框

转载 作者:行者123 更新时间:2023-12-02 20:16:41 26 4
gpt4 key购买 nike

我有以下复选框列表,它是从服务器以 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/

关于javascript - 通过比较值来选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6186582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com