如何使用Javascript为无线组编写警报代码
我现在有一个问题在我的HTML和一个简单的是或否广播组。我想让它这样,当用户回答是,他们会得到一个提示,说干得好。当他们回答“否”时,警报会显示“重试”并重新加载页面 这是html如何使用Javascript为无线组编写警报代码,javascript,Javascript,我现在有一个问题在我的HTML和一个简单的是或否广播组。我想让它这样,当用户回答是,他们会得到一个提示,说干得好。当他们回答“否”时,警报会显示“重试”并重新加载页面 这是html <div id="enterText"></div> <img src="../img/Window1.jpg" width="500" height="300"><br> <input type="radio" name="radio-group1"
<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!")
}
}
if(document.title==“3级”){
document.getElementById(“enterText”).innerHTML=“看看下面的图片,这个等式可能是真的吗?”;
document.getElementById(“submitBtn”).addEventListener(“单击”,calcChoices,false);
}
函数calcChoices(){
var-radioGroup=document.getElementsByName(“radio-group1”);
对于(变量i=0;i
为您的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!")
}
}
}
}
document.getElementById(“enterText”).innerHTML=“看看下面的图片,这个等式可能是真的吗?”;
document.getElementById(“submitBtn”).addEventListener(“单击”,calcChoices,false);
函数calcChoices(){
var-radioGroup=document.getElementsByName(“radio-group1”);
对于(变量i=0;i
我修改了您的代码,发现有几处错误。你的第一个if语句没有返回true,所以我添加了一个标题。你的最后两个if语句,我把它们连在一起了。这两个if语句不在for循环中。我修复了一些常见的布尔和分号错误。以下是最终结果:
if(document.title==“3级”){
document.getElementById(“enterText”).innerHTML=“看看下面的图片,这个等式可能是真的吗?”;
document.getElementById(“submitBtn”).addEventListener(“单击”,calcChoices,false);
}
函数calcChoices(){
var-radioGroup=document.getElementsByName(“radio-group1”);
对于(变量i=0;i
三级
对
不
提交
如果他们说“不”,此代码将重新加载页面。此外,修复了每次都说选择“是”的小错误
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!");
location.reload(); //Added this line
}
}
}
}
if(document.title==“3级”){
document.getElementById(“enterText”).innerHTML=“看看下面的图片,这个等式可能是真的吗?”;
document.getElementById(“submitBtn”).addEventListener(“单击”,calcChoices,false);
}
函数calcChoices(){
var-radioGroup=document.getElementsByName(“radio-group1”);
对于(变量i=0;i
太棒了!这仅仅是=和===之间的简单区别,对吗?是的,这几乎就是我所改变的:)@AlexZai他还嵌套了if语句,并将它们放入for循环中。location.reload()
以重新加载页面