检查是否输入了文本.找不到错误

checking if text is entered. Could not find error

本文关键字:找不到 错误 文本 是否 输入 检查      更新时间:2023-09-26

我正在尝试控制是否输入了文本这是我的代码,但它不起作用我找不到错误的

<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>

不要使用"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>

尝试

var x=document.getElementById("name").value;
if (x == '') { alert('message'); } // instead of null thanks @minmin

代码中缺少表单属性,这就是为什么它不能在中工作