简体   繁体   English

我不知道我的 JavaScript 函数不起作用

[英]I can't figure out my JavaScript function is not working

I have to create a game where the user has to guess a number, for that I call a JavaScript function on an external file which is activated when the button is clicked.我必须创建一个游戏,用户必须在其中猜测一个数字,为​​此我在单击按钮时激活的外部文件上调用 JavaScript 函数。

My HTML :我的 HTML

` `

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Devoir JavaScript</title>
  </head>
  <body>
    <p>Vous devez trouver un nombre compris entre 1 et 100 !</p>
    <form name="myForm" method="POST"></form>
      Nombre : <input type="text" name="nombre" value="" />
      <input type="button" onclick="return verif()" value="Jouer" />
    </form>
    <script type="text/javascript" src="script.js"></script>
  </body>
</html>

` My JavaScript : `我的 JavaScript

var numero = Math.floor(Math.random() * 100) + 1;
var essai = 0;
function verif() {
  essai++;
  var choix = document.forms["myForm"]["choix"].value;
  if (choix == null || choix == "") {
    alert("Indiquer un nombre !");
    return false;
  }
  if (choix < numero) {
    alert = "Le nombre indiquer est trop petit !";
    return false;
  }
  if (choix > numero) {
    alert = "Le nombre indiquer est trop grand !";
    return false;
  }
  if (choix == numero) {
    alert = "Bravo vous avez trouver en " + essai + "essais !";
    return true;
  }
}

I have checked the name of my files and their extentions so as not to make a mistake.我已经检查了我的文件的名称及其扩展名,以免出错。 I don't understand or I made a mistake.我不明白或我犯了一个错误。 PS: I am new to JavaScript. PS:我是 JavaScript 新手。 I have been looking since yesterday so thank you for your help.我从昨天开始一直在找,所以谢谢你的帮助。

I tried running your code and inside Console it says the value could not be read.我尝试运行您的代码,在 Console 中它说无法读取该值。 I slightly changed your Javascript code and it worked.我稍微更改了您的 Javascript 代码并且它起作用了。

//var choix = document.forms["myForm"]["choix"].value; code before

var choix = document.getElementsByName("nombre").value; //after changed

When using Javascript if your code is not running, you should press F12 and look at the Console to see what your code is failing.如果您的代码没有运行,则在使用 Javascript 时,您应该按 F12 并查看控制台以查看您的代码失败的地方。

This is the full code that I changed:这是我更改的完整代码:

 var numero = Math.floor(Math.random() * 100) + 1; var essai = 0; function verif() { essai++; var choix = document.getElementsByName("choix").value; document.write(choix); if (choix == null || choix == "") { alert("Indiquer un nombre !"); return false; } if (choix < numero) { alert = "Le nombre indiquer est trop petit !"; return false; } if (choix > numero) { alert = "Le nombre indiquer est trop grand !"; return false; } if (choix == numero) { alert = "Bravo vous avez trouver en " + essai + "essais !"; return true; } }
 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Devoir JavaScript</title> </head> <body> <p>Vous devez trouver un nombre compris entre 1 et 100 !</p> <form name="myForm" method="POST"></form> Nombre : <input type="number" name="choix" value=""/> <input type="button" onclick="return verif()" value="Jouer" /> </form> </body> </html>

Your error says that you are accessing the property value from undefined .您的错误表明您正在从undefined访问属性value There is no "choix" in your HTML.您的 HTML 中没有“choix”。

You can use indices to get your particular element value:您可以使用索引来获取您的特定元素值:

 var choix = document.forms[0].elements[0].value;

Or keeping the same approach as yours, I can use the correct field "nombre" :或者保持与您相同的方法,我可以使用正确的字段 "nombre" :

 var choix = document.forms["myForm"]["nombre"].value;

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

相关问题 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 我的 javascript 验证刚刚停止工作,我不知道为什么 - My javascript validation just stopped working and I can't figure out why 编码新手,我不明白为什么我的 javascript 按钮无法正常工作? - New to coding,I can't figure out why my javascript buttons are not working properly? 我的简单路由无法正常工作,我不知道为什么 - My simple routing is not working and I can't figure out why 我不知道为什么我的JavaScript无法正常工作…我正在将值动态传递给标签 - I can't figure out why my javascript isn't working… I'm dynamically passing values to a tag 我不知道的JavaScript难题 - JavaScript Puzzle I can't figure out 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 无法弄清楚为什么javascript无法在我的php文件中工作 - Can't figure out why javascript isn't working in my php file 我的 javascript 代码不会将元素推送到我的数组,我不知道为什么? - My javascript code won't push elements to my array and I can't figure out why? 无法弄清楚如何将我的 JavaScript 链接到我的 html 中 - Can't figure out how to link my JavaScript into my html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM