我正在使用$ .each循环生成多个复选框,并为它们提供了动态ID。单击复选框后,我想将复选框的ID传递给函数,并查看单击了哪个复选框,并显示警告框“我被单击”。数据来自JSON对象“ questions”。
startQuiz = function() {
$container = $('#container');
$quizArray = '';
$.each(questions, function(i, q) {
if(q.id == $index) {
$.each(q.options, function(j, a) {
$quizArray += '<ul>';
$quizArray += '<li>';
$quizArray += '<input type="checkbox" id="' + a.ans + '" onclick="clickFunc(\'' + q.id + '\', \''+ a.no +'\', \''+ a.ans +'\')"';
$quizArray += '" value="';
$quizArray += a.ans;
$quizArray += '" /><label for="' + a.ans + '">';
$quizArray += a.ans;
$quizArray += '</label></li>';
$quizArray += '</ul>';
});
}
});
$container.append($quizArray);
}
clickFunc = function($id, $no, $ans) {
$.each(questions, function(x, data) {
if(data.id == $id) {
$.each(data.correct, function(y, data2) {
if($('#'+$ans).is(':checked') && data2.no == $no) {
alert("I am clicked");
}
});
}
});
}