我的表格:
<form method="post" action="getQuote.php" onsubmit="check()">
<table>
<tr>
<td>Name:</td>
<td>
<input id="idname" class="inputBox" name="name"/>
</td>
</tr>
<tr>
<td>Phone Number:</td>
<td>
<input id="idphone" class="inputBox" name="phone" />
</td>
</tr>
<tr>
<td>Moving From:</td>
<td>
<input id="idfrom" class="inputBox" name="from" />
</td>
</tr>
<tr>
<td>Moving To:</td>
<td>
<input id="idto" class="inputBox" name="to" />
</td>
</tr>
<tr>
<td>Date:</td>
<td>
<input id="iddate" class="inputBox" name="date" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td></td>
<td>
<button name="signup" class="myButton">
<font style="color: #ffffff">Get A Quote</font>
</button>
</td>
</tr>
</table>
</form>
我的脚本:
<script>
function check() {
var n = document.getElementById(idname);
var p = document.getElementById(idphone);
var f = document.getElementById(idfrom);
var t = document.getElementById(idto);
var d = document.getElementById(iddate);
var test1 = n.length;
var test2 = p.length;
var test3 = f.length;
var test4 = t.length;
var test5 = d.length;
alert(test1);
alert(test2);
alert(test3);
alert(test4);
alert(test5);
}
</script>
我将脚本称为表单的onsubmit函数,但页面未显示警告。相反,它将转到getQuote.php文件。谁能告诉我,这是什么问题?我认为,我缺少一些愚蠢的东西。
N.B:无论值是否为空,我都没有编写要比较的部分。只是检查值。
提前致谢。
最佳答案
尝试一下
<script>
function check() {
var n = document.getElementById('idname');
var p = document.getElementById('idphone');
var f = document.getElementById('idfrom');
var t = document.getElementById('idto');
var d = document.getElementById('iddate');
var test1 = n.length;
var test2 = p.length;
var test3 = f.length;
var test4 = t.length;
var test5 = d.length;
alert(test1);
alert(test2);
alert(test3);
alert(test4);
alert(test5);
}
</script>