我如何更改此 Javascript 代码来验证数据
我需要添加什么才能根据已选择的复选框数量进行验证?我希望用户在提交数据之前至少选择两个复选框。这是我的 Javascript 代码:
<script type="text/javascript" language="JavaScript">
function checkCheckBoxes(theForm) {
if (
theForm.Conservatives.checked == false &&
theForm.Labour.checked == false &&
theForm.LiberalDemocrats.checked == false)
{
alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
return false;
} else {
return true;
}
}
</script>
当前的 Javascript 代码仅验证是否选中了任何复选框,但我希望它验证两个复选框。
What would I need to add in order for this to validate according to how many checkboxes have been selected? I want the user to select at least two checkboxes before submission of data. Here is my Javascript code:
<script type="text/javascript" language="JavaScript">
function checkCheckBoxes(theForm) {
if (
theForm.Conservatives.checked == false &&
theForm.Labour.checked == false &&
theForm.LiberalDemocrats.checked == false)
{
alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
return false;
} else {
return true;
}
}
</script>
The current Javascript code only validates if any checkboxes have been selected or not, but I want it to validate for two checkboxes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
数一下检查了多少个,看看是否小于2个。
Just count how many are checked and see if it's less than 2.
只要您只担心这三个复选框并且不想使用 JavaScript 库,我能想到的最简单的事情就是:
此方法不仅会为您提供选中项目的计数,而且还会为您提供已选中项目的计数。如果需要的话,还允许您仅访问代码中稍后的复选框。
As long as you're only worried about those three checkboxes and you don't want to use a JavaScript library, the easiest thing I can think of would be:
This method will not only give you the Count of the number of checked items but will also allow you to access only the checked boxes later in your code if needed.
你可以这样做:
You can do: