为什么我的代码是's 'e'没有定义

Why is my code's 'e' not defined?

本文关键字:定义 代码 为什么 我的      更新时间:2023-09-26

我想评论一下用户的输入。在我的代码e是一个函数,在onclick属性我们写一个函数,所以我错了,请帮助。

<form>
  What color do you prefer?<br>
  <input type="radio" id="userInput" >Red<br>
  <input type="radio" id="userInputs" >Blue
</form>
<button onclick="e()">far</button>
<script>
var Input = document.getElementById("userInputss"); 
document.write(userInputs);
Function e(){
if(userInputss===userInput){
document.write("good");}
else if(Input===userInputs){
document.write("go");}
}
</script>
</body>
</html>

对我来说不是很干净,你想在e函数中做什么,但我建立了这个例子来帮助你:

var red = document.getElementById("colorRed")
  , blue = document.getElementById("colorBlue"); 
//document.write(red);
function e(){
  if(blue.checked == true){
    document.write("You prefer Blue");
  } else if (red.checked == true){
    document.write("You prefer Red");  
  } else {
    document.write("You prefer nothing");  
  }
}
<form>
  What color do you prefer?<br>
  <input type="radio" id="colorRed" >Red</input>
  <input type="radio" id="colorBlue" >Blue</input>
</form>
<button onclick="e()">far</button>

因为你的代码不是在<script>function应该是小写的。试试这个:

<form>
  What color do you prefer?<br>
  <input type="radio" id="userInput" >Red<br>
  <input type="radio" id="userInputs" >Blue
</form>
<button onclick="e()">far</button>
<script>
var Input = document.getElementById("userInputs"); 
document.write(userInputs);
function e(){
  console.log("Click");
  /* I'm not sure what that is supposed to do:
  if(userInputs===userInput){
    document.write("good");
  } else if(Input===userInputs){
    document.write("go");
  }*/
}
</script>
</body>
</html>

查看控制台(F12 ->控制台),查看点击。