所以在html体内我都有
<script type="text/javascript">
function validateMelco(form) {
toReturn=true;
if (form.melco.value.length==0) {
alert("Please enter your Melco serial no");
toReturn=false;
}
return toReturn;
}
</script>
和
<tr>
<td valign="top"><b>SongKong for Melco (Melco discount)</b>
</td>
<td valign="top">£40 ($50 USD)
</td>
<td valign="top">
<form name="paypalpro" action="https://www.paypal.com/cgi-bin/webscr" onsubmit="return validateMelco()" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="5UC5NAYZ6JZR8">
<input alt="PayPal" type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" border="0" name="submit">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
Melco Serial No:<input type="textfield" name="melco"/>
</form>
</td>
</tr>
然而,用户可以单击该按钮而不在输入字段中输入任何内容,不显示警报并且发布表单。为什么JavaScript验证没有开始?
答案 0 :(得分:1)
更改行
<form name="paypalpro" action="https://www.paypal.com/cgi-bin/webscr" onsubmit="return validateMelco()" method="post">
到
<form name="paypalpro" action="https://www.paypal.com/cgi-bin/webscr" onsubmit="return validateMelco(this)" method="post">
答案 1 :(得分:1)
您在没有参数的情况下调用该函数,但它需要一个标识该表单的参数。所以它什么都做不了。
答案 2 :(得分:1)
<!DOCTYPE html>
<html>
<body>
<p>When you submit the form, a function is triggered which alerts some text.</p>
<tr>
<td valign="top"><b>SongKong for Melco (Melco discount)</b>
</td>
<td valign="top">£40 ($50 USD)
</td>
<td valign="top">
<form name="paypalpro" action="https://www.paypal.com/cgi-bin/webscr" onsubmit="return validateMelco()" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="5UC5NAYZ6JZR8">
<input alt="PayPal" type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" border="0" name="submit">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
Melco Serial No:<input type="textfield" name="melco"/>
</form>
</td>
</tr>
<script>
function validateMelco(form) {
toReturn=true;
if (document.forms["paypalpro"]["melco"].value == "") {
alert("Please enter your Melco serial no");
toReturn=false;
}
return toReturn;
}
</script>
</body>
</html>
表单对象尚未传递给函数“validateMelco()”以访问表单元素。所以我使用javascript文档对象使用'document.forms [“paypalpro”] [“melco”]'来访问表单。