[英]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.