[英]If statement inside function on Javascript
我有一个小项目,我试图让 function 在文本框1 中的数字小于 10 时在文本框上显示文本,如果数字大于 10 则显示其他文本.. 尝试如下但如果有人可以提供帮助则不起作用?
<script>
function hello (){
var x = document.getElementById.(Text1).value;
if (x>10){
document.getElementById(Text3).value = "1";
}
else {
document.getElementById(Text3).value = "2";
}
}
</script>
<body>
<input type="text" id="Text1" />
<br/>
<input type="button" onclick="hello();" value="Click..."/>
<input type="text" id="Text3"/>
</body>
如果您正在引用 DOM 元素:
<script> function hello (){ var x = document.getElementById("Text1").value; if (x>10){ document.getElementById("Text3").value= "1"; } else { document.getElementById("Text3").value= "2"; } } </script> <body> <input type="text" id="Text1" /> <br/> <input type="button" onclick="hello()" value="Click..."/> <input type="text" id="Text3"/> </body>
您正在做很多语法错误。 首先,您的document.getElementById()
中有这么多点 (.) 将其删除,并且document.getElementById(Text1)
的值应该是引号'
或双引号'
之间document.getElementById('Text1')
。 我已经修复了所有东西,现在它会很好用。 以下是您的固定代码。
<script>
function hello (){
var x = document.getElementById('Text1').value;
if (x>10){
document.getElementById('Text3').value = "1";
} else {
document.getElementById('Text3').value = "2";
}
}
</script>
<body>
<input type="text" id="Text1" />
<br/>
<input type="button" onclick="hello()" value="Click..."/>
<input type="text" id="Text3"/>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.