Javascript 检查条件后表单验证未发出警报

Javascript 检查条件后表单验证未发出警报,javascript,html,forms,validation,Javascript,Html,Forms,Validation,我试图用javascript验证HTML表单,但由于没有弹出警报,我认为没有调用我的checkSubmit()函数。但是,在功能开始时添加额外警报后,它确实会触发,但在检查第一个条件后,不会弹出进一步的警报 <script language="javascript" type="text/javascript"> function checkSubmit() { alert("Hello from Javascript!"); if (document.ge

我试图用javascript验证HTML表单,但由于没有弹出警报,我认为没有调用我的checkSubmit()函数。但是,在功能开始时添加额外警报后,它确实会触发,但在检查第一个条件后,不会弹出进一步的警报

<script language="javascript" type="text/javascript">
  function checkSubmit() {

     alert("Hello from Javascript!");
     if (document.getElementById("user").value.toString.length <1) {
        alert("Please enter a user name.");
        return false;
     }

     if (document.getElementById("location").value.toString.length <1) {
        alert("Please enter a location.");
        return false;
     }
     if (document.getElementById("depart").value.toString.length <1) {
        alert("Please enter a department.");
        return false;
     }
     if (document.getElementById("category").value.toString.length <1) {
        alert("Please enter a problem type.");
        return false;
     }
     if (document.getElementById("info").value.toString.length <1) {
        alert("Please enter a problem description.");
        return false;
     }
     alert("Hello from Javascript2!");
     return true;
   }   
</script>

<form name="submit" type="submit" method="POST" onsubmit="return checkSubmit();">

   [...]

   <input type="submit" name="submit" value="Submit" >
</form>

函数checkSubmit(){
警报(“来自Javascript的你好!”);

如果(document.getElementById(“user”).value.toString.length您不需要
toString
,因为
.value
将已经是一个字符串,并且您仍然错误地使用了它-您需要说
.toString()
。实际上,它返回函数定义,因此
.length
将为
0
。正如您所说,您需要为每个框正确设置
id
属性

function checkSubmit() {

    if (document.getElementById("user").value.length < 1) {
        alert("Please enter a user name.");
        return false;
     }

     if (document.getElementById("location").value.length < 1) {
        alert("Please enter a location.");
        return false;
     }

     if (document.getElementById("depart").value.length < 1) {
        alert("Please enter a department.");
        return false;
     }

     if (document.getElementById("category").value.length < 1) {
        alert("Please enter a problem type.");
        return false;
     }

     if (document.getElementById("info").value.length < 1) {
        alert("Please enter a problem description.");
        return false;
     }

     return true;
}
函数checkSubmit(){
if(document.getElementById(“用户”).value.length<1){
警报(“请输入用户名”);
返回false;
}
if(document.getElementById(“位置”).value.length<1){
警报(“请输入位置”);
返回false;
}
if(document.getElementById(“离开”).value.length<1){
警告(“请进入一个部门”);
返回false;
}
if(document.getElementById(“category”).value.length<1){
警报(“请输入问题类型”);
返回false;
}
if(document.getElementById(“info”).value.length<1){
警报(“请输入问题描述”);
返回false;
}
返回true;
}

您是否在控制台中报告了一个错误?可能找不到您的某个控件…我猜您遇到了一个错误,例如未定义值。啊哈。我在第一个输入框中缺少了“id=”标记。但是,现在我遇到了相反的问题-我收到了一个警告,要求输入用户名,即使在输入id为“user”的框时也是如此有文本。谢谢。现在可以了。希望这个问题能帮助那些认为自己的“onsubmit”不起作用的死路一条的人,因为页面最初看起来是提交/刷新的,没有任何其他动作。