gpt4 book ai didi

javascript - 无法输出值: JavaScript

转载 作者:行者123 更新时间:2023-12-02 19:26:30 25 4
gpt4 key购买 nike

我试图在文本框中输入一个数字,并通过 switch 功能在另一个 div 标签中将消息显示为当天的名称。我传递给 switch 函数的变量必须从文本框中获取值。但我无法在单击按钮时输出该函数,它显示消息“未定义”。请帮助 :)。谢谢

<input type = "text" id = "test" />
<br>
<div id="box3" style = "border:solid 2px blue; height:15px;width:100px;"></div>
<br>
<input type = "button" onClick = "myTest()" value = "Click me" />

<script type="text/javascript">
function myTest(){
var x;
var d =document.getElementById('test').value;
switch (d)
{
case 0:
x="Today it's Sunday";
break;
case 1:
x="Today it's Monday";
break;
case 2:
x="Today it's Tuesday";
break;
case 3:
x="Today it's Wednesday";
break;
case 4:
x="Today it's Thursday";
break;
case 5:
x="Today it's Friday";
break;
case 6:
x="Today it's Saturday";
break;
}
document.getElementById('box3').innerHTML=x;
}

</script>

最佳答案

您需要先将其转换为整数:

var d = parseInt(document.getElementById('test').value);

关于javascript - 无法输出值: JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11989584/

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