[英]How can I make my selected outstanding in the form?
I have created two questions in the tag.我在标签中创建了两个问题。 I want to highlight the answer I chose by changing the background color.
我想通过更改背景颜色来突出显示我选择的答案。 However, it is possible for me to do this because when I choose the answer in the first question, then I choose the next question, the highlight in the previous one disappears.
但是,我可以这样做,因为当我在第一个问题中选择答案,然后选择下一个问题时,前一个问题中的突出显示消失了。 So now, how I can do that, and how I can choose my answer by click anywhere in the answer box instead of clicking on the label or the radio box.
那么现在,我该怎么做,以及如何通过单击答案框中的任意位置而不是单击 label 或单选框来选择我的答案。
function openPresent(event) { const answer = document.querySelectorAll('.option'); for (let i = 0; i < answer.length; i++) { answer[i].style.backgroundColor = originalAnswerArray[i]; } event.target.style.backgroundColor = 'blue'; } const answer = document.querySelectorAll('.option'); let i; let originalAnswerArray = []; for (i = 0; i < answer.length; i++) { originalAnswerArray.push(answer[i].getAttribute('value')); answer[i].addEventListener('click', openPresent); }
.option { border-top: 1px solid white; border-bottom: 1px solid white; display: flex; justify-content: flex-start; align-items: center; flex-direction: row; width: auto; padding: 15px; background-color: #f1f1f1; }.option:hover { background-color: #ddd; } label { margin-left: 15px; }
<form> <div class='option'> <input type='radio' id='option1' name="question1" value="option 1"> <label for='option1'> option 1 </label><br> </div> <div class='option'> <input type='radio' id='option2' name="question1" value="option 2"> <label for='option2'>option 2</label><br> </div> <div class='option'> <input type='radio' id='option3' name="question1" value="option 3"> <label for='option3'>option 3</label><br> </div> <div class='option'> <input type='radio' id='option4' name="question1" value="option 4"> <label for='option4'>option 4</label><br> </div> </form> <p><strong> Question 2 of 10</strong></p> <p>Question 2</p> <form> <div class='option'> <input type='radio' id='option1' name="question2" value="option 1"> <label for='option1'> <div>option 1 </div> </label><br> </div> <div class='option'> <input type='radio' id='option2' name="question2" value="option 2"> <label for='option2'>option 2</label><br> </div> <div class='option'> <input type='radio' id='option3' name="question2" value="option 3"> <label for='option3'>option 3</label><br> </div> <div class='option'> <input type='radio' id='option4' name="question2" value="option 4"> <label for='option4'>option 4</label><br> </div> </form>
The function openPresent
with property event, will produce the pointer event in console log.带有属性事件的 function
openPresent
,将在控制台日志中产生指针事件。 You can use the object of target
( event.target
) to search your input element.您可以使用
target
( event.target
) 的 object 来搜索您的输入元素。 You can use querySelector
.您可以使用
querySelector
。 After that, you can change the checked to true for make this radio input checked.之后,您可以将 checked 更改为 true 以选中此无线电输入。 You can use this code:
您可以使用此代码:
function openPresent(event) {
const answer = document.querySelectorAll(".option");
for (let i = 0; i < answer.length; i++) {
answer[i].style.backgroundColor = originalAnswerArray[i];
}
const input = event.target.querySelector('input')
input.checked = true
event.target.style.backgroundColor = "blue";
}
const answer = document.querySelectorAll(".option");
let i;
let originalAnswerArray = [];
for (i = 0; i < answer.length; i++) {
originalAnswerArray.push(answer[i].getAttribute("value"));
answer[i].addEventListener("click", openPresent);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.