首先,我没有HTML控件。我在某处的 for 循环中出错,好像我只为一个 id 做,它工作正常。
for (var i = 1; i < 73; i++) {
$("#a" + i).click(function() {
if ($("#a" + i).hasClass("green")) {
$("#Q15v2_" + i).prop('checked', true);
} else {
$("#Q15v2_" + i).prop('checked', false);
}
});
}
试试这个,让我知道:
检查此 https://jsfiddle.net/shantaram/g1x7rh25/
$("p[id^=a").click(function() { // paragraph id start with 'a'
var len = $(this).prop('id').length; // calculate id length
var i = $(this).prop('id').substr(1,len); // remove 'a' from id to get number
if ($(this).hasClass("green")) {
$("#Q15v2_" + i).prop('checked', true);
} else {
$("#Q15v2_" + i).prop('checked', false);
}
});