gpt4 book ai didi

javascript - 如何使用 Javascript 为警报编码 radio 组

转载 作者:行者123 更新时间:2023-11-30 16:58:28 24 4
gpt4 key购买 nike

我目前在我的 HTML 和一个单选组中有一个简单的是或否的问题。我想做到这一点,以便当用户回答"is"时,他们会收到一条提示说做得好。当他们回答“否”时,警报会提示重试并重新加载页面。

这是html

    <div id="enterText"></div>

<img src="../img/Window1.jpg" width="500" height="300"><br>

<input type="radio" name="radio-group1" value="Yes">Yes
<input type="radio" name="radio-group1" value="No">No<br><br>
<button id="submitBtn">Submit</button>

这是我当前的 javascript

if (document.title === "Level 3"){

document.getElementById("enterText").innerHTML = "Look at the Picture below, Is it possible for the equation to be true?";

document.getElementById("submitBtn").addEventListener("click", calcChoices, false);
}



function calcChoices(){
var radioGroup = document.getElementsByName("radio-group1");
for (var i = 0; i < radioGroup.length; i++) {
if (radioGroup[i].checked){
console.log("You clicked: "+ radioGroup[i].value);
}
}
if(radioGroup[i].value="Yes"){
alert("NICE!")
}
if(radioGroup[i].value="No"){
alert("try again!")
}
}

最佳答案

为您的 javascript 尝试以下代码:

document.getElementById("enterText").innerHTML = "Look at the Picture below, Is it possible for the equation to be true?";

document.getElementById("submitBtn").addEventListener("click", calcChoices, false);


function calcChoices(){
var radioGroup = document.getElementsByName("radio-group1");
for (var i = 0; i < radioGroup.length; i++) {
if (radioGroup[i].checked) {
if(radioGroup[i].value==="Yes"){
alert("NICE!")
}
if(radioGroup[i].value==="No"){
alert("try again!")
}
}
}
}

关于javascript - 如何使用 Javascript 为警报编码 radio 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29270672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com