表单onSubmit返回false在Chrome中不起作用

Jor*_*ems 5 javascript google-chrome onsubmit

当我想通过javasript检查用户输入时,我的返回虚假声明在Chrome中无效.即使我返回false,它也会触发动作标记.这是我的代码

<script type="text/javascript">
  function testInput(){
    if(document.getElementById("input1").value == ""){
      alert('error');
      return false;
    }else{
      return true;
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

HTML

<form action="exec.php" method="post" onSubmit="return testInput()">
<input type="text" id="input1" name="input1" />
<input type="submit" value="Exec" />
</form>
Run Code Online (Sandbox Code Playgroud)

kir*_*nvj 0

您确定输入字段中没有空格吗?

尝试这样的 if(document.getElementById("input1").value.trim () == "")

请注意 tim 在 IE 中无法正常工作。如果需要,请使用此解决方案http://goo.gl/L802W