我正在尝试一个具有文本字段和复选框的表单。我是一名设计师,而不是一名开发人员。我已成功获取要验证的文本框,但我已粘贴了该复选框的代码,由于某种原因,该代码要么取消验证,要么在 PayPal 服务器上返回错误。任何人都可以建议几行代码来验证复选框“条款”吗?该文本框称为“os0”
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" id="myForm" onSubmit="return validateForm()" >
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="AXNKKDCZLVHM4">
<table width="100%" border="0">
<tr>
<th scope="row"><input type="hidden" name="on0" value="Your RCEH Account Number:" />
Your RCEH Account Number:
</th>
<td>
<input type="text" name="os0" maxlength="200" />
</td>
</tr>
<tr>
<th scope="row"> </th>
<td><label>
<input type="checkbox" name="terms" id="terms" />
Agree to <a href="../terms.html" target="new">terms and conditions</a></label>
</td>
</tr>
</table>
<input type=submit value="Proceed to secure server">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</div>
</div>
<script>
function validateForm() {
var x=document.forms["myForm"]["os0"].value;
if (x==null || x==""){
alert("RCEH account number must be filled out");
return false;
}
}
</script>
最佳答案
假设您只想选中复选框,下面是您需要的代码,这是一个有效的 JSFiddle .
function validateForm() {
var form = document.getElementById("myForm"); //get the form element via ID
if (form.os0.value == "")
{
alert("RCEH account number must be filled out");
return false;
}
if (!form.terms.checked) { // checks if the checkbox called "terms" is checked; if it is not, alerts a message and return false
alert("Terms must be read and approved");
return false;
}
return true; //if everything is okay, return true and submit the form
}
关于javascript - 勾选验证码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16748758/