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");}); 因?yàn)槲矣谐^(guò) 4 個(gè)按鈕答案,所以如何才能使其更加動(dòng)態(tài)?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() 只改變當(dāng)前取消/選擇按鈕的顏色我不知道為什么這不起作用。每次我點(diǎn)擊另一個(gè)按鈕或同一個(gè)按鈕時(shí),他都會(huì)打印出相同的按鈕 ID...
2 回答

蝴蝶不菲
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
讓所有按鈕都相同class="answer_btn"。然后你可以做
$(".answer_btn").click(function() {
select_answer(this);
$(".answer_btn").not(this).each(function() {
deselect_answer(this);
});
});

汪汪一只貓
TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊
? ? ? ? button.addEventListener("click", function() {
? ? ? ? selectAnswer(this);
? ? ? ? $(".exam-btn").not(this).each(function() {
? ? ? ? ? deselectAnswer(this);
? ? ? ? });
? ? ? ? console.log(this.id);
? ? });
- 2 回答
- 0 關(guān)注
- 115 瀏覽
添加回答
舉報(bào)
0/150
提交
取消