Javascript返回false,警报不起作用

时间:2012-08-17 16:48:52

标签: javascript forms return handler onsubmit

我正在尝试确保某些字段在我的表单中不会留空。它似乎很简单,但由于某种原因,它不起作用。警报未显示,并且返回false不起作用(它继续将空白条目发布到我的数据库中)请帮忙,我做错了什么。谢谢!

脚本:

function check(){
var name = getElementById('name');
var date = getElementById('date');
var pri = getElementById('pri');
var asapc = getElementById('asapc');
var asapn = getElementById('asapn');
var obr = getElementById('obr');
var obc = getElementById('obc');
var obn = getElementById('obn');
  if (name.value == "" || date.value == "" || pri.value == "not" || asapc.value == "" ||  asapn.value == "" || obr.value == "" || obc.value == "" || obn.value == "") {
    alert( "One or more fields were not filled out." );
    return false ; }
    return true;
 }

代码:

<FORM ACTION="step2.php" METHOD="POST" onsubmit="check();">
<!-- fields here -->
<INPUT TYPE="submit" VALUE="CONTINUE">

3 个答案:

答案 0 :(得分:5)

document.getElementById...访问每个元素,并在表单标记中写下onsubmit="return check();"而不是onsubmit="check();"

答案 1 :(得分:2)

你错过了这里的回报:

<FORM ACTION="step2.php" METHOD="POST" onsubmit="return check();">

答案 2 :(得分:0)

您缺少(文档)这是正确的语法:

document.getElementById('id');


<script>
function check() {
var name = document.getElementById('name');
var date = document.getElementById('date');
  if (name.value == "" || date.value == "") {
        alert( "One or more fields were not filled out.");
        return false; 
    }
}
</script>

和getElementByID表示ID不是我的tagName

<FORM ACTION="step2.php" METHOD="POST" onsubmit="return check();">
  <input name="name" type="text" value="" id="name">
  <input name="date" type="text" value="" id="date">
   .....etc..
  <INPUT TYPE="submit" VALUE="CONTINUE">
</FORM>