2013-08-28 56 views
6

當我提交表單時,我收到了一條警告消息。當我接受警報時,它仍然會提交表格。返回false將被忽略。 Onclick無法使用。我試着用var x = document.forms["form"]["fname"].value; 仍然是一樣的。關於提交表單,返回false不起作用

<form id="f" method="post" name="form" onsubmit="return validateForm();" action="#"> 
    <input type="text" name="fname" id="test" /> 
    <input type="submit" value="submit"/> 
</form> 
<script type="text/javascript"> 
     function validateForm() { 
      var x = document.getElementById('test').value; 
      if (x == null || x == 0 || x == "0") { 
       alert("Stop"); 
       return false; 
      } 
     } 
    </script> 
+3

它工作正常,請檢查您的瀏覽器控制檯的錯誤嘗試http://jsfiddle.net/arunpjohny/KJyF8/1 –

+0

做工精細這裏:http://jsfiddle.net/su4jr/ – hjpotter92

+0

外觀類似於

回答

9

代替<input type="submit" value="submit"/>使用<input type="button" value="Submit" onclick='validateForm()'/>

在您的JS:

<script type="text/javascript"> 
    function validateForm() { 
     var x = document.getElementById('test').value; 
     if (x == null || x == 0 || x == "0") { 
      alert("Stop"); 
     } 
     else 
      document.form.submit(); 
    } 
</script> 
+0

這個問題說onclick不能使用 – mechenbier

+0

這個問題是爲什麼不使用onclick和onclick什麼。 – Aashray

+0

Onclick on button is ok。On form is problem,because I have 4 buttons。 –

0

給這個腳本head標籤內進行檢查。

<script type="text/javascript"> 
     function validateForm() { 
      var x = document.getElementById('test').value; 
      if (x == null || x == 0 || x == "0") { 
       alert("Stop"); 
       return false; 
      } 
     } 
</script> 
相關問題