JavaScript$("#answer_a_btn").click(function() { selectAnswer("#answer_a_btn"); deselectAnswer("#answer_b_btn"); deselectAnswer("#answer_c_btn"); deselectAnswer("#answer_d_btn");});$("#answer_b_btn").click(function() { deselectAnswer("#answer_a_btn"); selectAnswer("#answer_b_btn"); deselectAnswer("#answer_c_btn"); deselectAnswer("#answer_d_btn");});$("#answer_c_btn").click(function() { deselectAnswer("#answer_a_btn"); deselectAnswer("#answer_b_btn"); selectAnswer("#answer_c_btn"); deselectAnswer("#answer_d_btn");});$("#answer_d_btn").click(function() { deselectAnswer("#answer_a_btn"); deselectAnswer("#answer_b_btn"); deselectAnswer("#answer_c_btn"); selectAnswer("#answer_d_btn");}); 因为我有超过 4 个按钮答案,所以如何才能使其更加动态?function selectOnlyOneExamButton(examButtonId, examButtonsIds) { for(var i = 0; i < examButtonsIds.length; i++) { if(examButtonsIds[i] == examButtonId) { console.log("SELECTED ANSWER " + examButtonsIds[i]); selectAnswer(examButtonsIds[i]); } else { console.log("DESELECTED ANSWER " + examButtonsIds[i]); deselectAnswer(examButtonsIds[i]); } }};de-/selectAnswer() 只改变当前取消/选择按钮的颜色我不知道为什么这不起作用。每次我点击另一个按钮或同一个按钮时,他都会打印出相同的按钮 ID...
2 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
让所有按钮都相同class="answer_btn"。然后你可以做
$(".answer_btn").click(function() {
select_answer(this);
$(".answer_btn").not(this).each(function() {
deselect_answer(this);
});
});
汪汪一只猫
TA贡献1898条经验 获得超8个赞
button.addEventListener("click", function() {
selectAnswer(this);
$(".exam-btn").not(this).each(function() {
deselectAnswer(this);
});
console.log(this.id);
});
- 2 回答
- 0 关注
- 73 浏览
添加回答
举报
0/150
提交
取消