简体   繁体   English

我的脚本有什么问题? 它不返回带有“ if语句”的消息

[英]What is wrong with my script? It doesn't return a message with the “if statement”

i've made this script but it won't work properly, what i want is an input box, and if i use it the site returns some text. 我已经制作了此脚本,但无法正常工作,我想要的是一个输入框,如果我使用它,该网站将返回一些文本。

 function myFunction(); var text_input = document.getElementById("input001").value; if (text_input == "4") { document.getElementById("message001").innerText = "Correct!"; } else { document.getElementById("message001").innerText = "nope"; } } 
 <div class=questions> <p>What is 9-5?</p> </div> <input id="input001" type="text" /> <button onclick="myFunction()">Check</button> <p id="message001"></p> 

The problem is you are closing your function with a semi colon. 问题是您正在用半冒号关闭函数。

Instead, put an opening parenthesis 取而代之的是放一个圆括号

function myFunction(){
        var text_input=document.getElementById("input001").value;
        if (text_input=="4"){
            document.getElementById("message001").innerText="Correct!";
            }
        else{
            document.getElementById("message001").innerText="nope";
            }
     }
function myFunction(){//changed line no need to add ; here
var text_input = document.getElementById("input001").value;
 if (text_input == "4") {
   document.getElementById("message001").innerText = "Correct!";
  } else {
   document.getElementById("message001").innerText = "nope";
  }
}

You have the wrong format for function definition 您的function定义格式错误

you just need { after creating a function for the scope 您只需要{在为范围创建函数后

function myFunction(){
   // your code
}

 function myFunction(){ var text_input=document.getElementById("input001").value; if (text_input=="4"){ document.getElementById("message001").innerText="Correct!" } else{ document.getElementById("message001").innerText="nope" } } 
 <!--first question--> <div class=questions> <p>What is 9-5?</p> </div> <input id="input001" type="text" /> <button onclick="myFunction()">Check</button> <p id="message001"></p> 

 function myFunction() { var text_input = document.getElementById("input001").value; if (text_input == "4") { document.getElementById("message001").innerText = "Correct!"; } else { document.getElementById("message001").innerText = "nope"; } } 
 <div class=questions> <p>What is 9-5?</p> </div> <input id="input001" type="text" /> <button onclick="myFunction()">Check</button> <p id="message001"></p> 

You have syntax errors. 您有语法错误。 Find This Example, It will work. 查找此示例,它将起作用。

Apart from the fact that you declared only a function with an error (notice a semicolon right after function name), your code doesn't work because you are not passing a function in button's onclick attribute - you're just passing the return value of that function there. 除了您只声明了一个带有错误的函数(注意在函数名后紧跟一个分号)外,您的代码也不起作用,因为您没有在button的onclick属性中传递函数-您只是传递的返回值。那里的功能。 You should have <button onclick="myFunction">Check</button> there instead. 您应该在那里改用<button onclick="myFunction">Check</button>

Below the correct code: 在正确的代码下面:

function myFunction() {
  var text_input = document.getElementById("input001").value;
  if (text_input == "4") {
    document.getElementById("message001").innerText = "Correct!";
  } else {
    document.getElementById("message001").innerText = "nope";
  }
}
<div class=questions>
  <p>What is 9-5?</p>
</div>

<input id="input001" type="text" />
<button onclick="myFunction">Check</button>
<p id="message001"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM