gpt4 book ai didi

Javascript 比较功能不起作用

转载 作者:行者123 更新时间:2023-11-29 16:13:44 25 4
gpt4 key购买 nike

你能告诉我为什么这段代码不起作用吗?我将值 1 或值 2 放入 text 元素 但我没有得到结果“this is 1”或“this分别为 2"。似乎行 "x = document.getElementById("value").value;"不能正常工作,因为在调试期间我放了 n=1; 和它工作完美

<html>
<body>
<p>Check the value</p>
<input id="value" type="text"></input>
<button type="button" onclick="myFunction()">Check</button>
<p id="demo"></p>
<script>
function myFunction()
{
var x;
x = document.getElementById("value").value;
switch(x)
{
case 1:
document.getElementById("demo").innerHTML = "this is 1";
break;
case 2:
document.getElementById("demo").innerHTML = "this is 2";
break;
}
}
</script>
</body>
</html>

最佳答案

输入值是字符串。您正在比较字符串 "1" 和数字 1,它们是不同的。

关于Javascript 比较功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20901931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com