我试图控制是否输入文本是我的代码而且它不起作用我找不到什么错误
<html>
<head>
<script type="text/javascript">
function CheckInfos()
{
var x=document.forms("name").value;
if(x==null)
{
alert("adınızı kontrol ediniz");
}
}
</script>
</head>
<body>
<input type="text" id="name" />
<input type="button" value="test" onclick="CheckInfos()"/>
</body>
答案 0 :(得分:3)
请勿使用“name”作为ID,但请使用getElementById。
var value = document.getElementById("foo").value;
但你应该使用一个表格:
<form name="myform" action="?" method="get" onsubmit="return CheckInfos(this)">
<input type="text" name="foo" />
<input type="submit" value="test"/>
</form>
<script>
function CheckInfos(form) {
if (form.elements.foo.value == "") {
alert("error");
}
return false;
}
</script>
答案 1 :(得分:2)
尝试
var x=document.getElementById("name").value;
if (x == '') { alert('message'); } // instead of null thanks @minmin
答案 2 :(得分:0)
代码中缺少form属性,说明它无效的原因