【发布时间】:2012-12-19 09:27:20
【问题描述】:
我想验证我的表单。我的代码点火器视图中有一个表单验证 javascript 代码,但它不工作,并且仍然在没有任何验证的情况下将值发送到下一页。你能告诉我我在哪里犯错以及为什么这样正在发生吗?
代码:
<form method="get" action="/calculator/stage_two" name="calculate" id="calculate" onsubmit="return validateForm();">
<div class="calc_instruction">
<input type="text" name="property_number" placeholder="No/Name" id = "property_number" class="stage_one_box" />
<input type="text" name="postcode" placeholder="Postcode" id = "postcode" class="stage_one_box" />
</div>
<input type = "image" name = "submit_calculator" id = "submit_calculator" value="Go" src = "/images/next_button.png" />
</form>
Javascript 函数:
<script type="text/javascript">
function validateForm() {
var postcode=document.forms["calculate"]["postcode"].value;
if (postcode==null || postcode=="") {
alert("Please enter the postcode to give you more accurate results");
document.forms["calculate"]["postcode"].focus();
return false;
}
</script>
【问题讨论】:
-
你收到警报了吗?
-
保持结构清洁非常重要!那么像缺少括号这样的错字就不会再经常发生了..
-
如果你使用的是mozilla,出现错误后按(ctrl+shift+j)。这将帮助您进行调试。
-
干杯,伙计们。已排序。我缺少括号并且没有使用 getElementById。谢谢。
标签: php javascript html codeigniter