我如何更改此 Javascript 代码来验证数据

发布于 2025-01-08 01:03:14 字数 585 浏览 1 评论 0原文

我需要添加什么才能根据已选择的复选框数量进行验证?我希望用户在提交数据之前至少选择两个复选框。这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

厌味 2025-01-15 01:03:14

数一下检查了多少个,看看是否小于2个。

function checkCheckBoxes(theForm) {
    var cnt = 0;
    if (theForm.Conservatives.checked) ++cnt;
    if (theForm.Labour.checked) ++cnt;
    if (theForm.LiberalDemocrats.checked) ++cnt;
    if (cnt < 2) {
        alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
        return false;
    } else {    
        return true;
    }
}

Just count how many are checked and see if it's less than 2.

function checkCheckBoxes(theForm) {
    var cnt = 0;
    if (theForm.Conservatives.checked) ++cnt;
    if (theForm.Labour.checked) ++cnt;
    if (theForm.LiberalDemocrats.checked) ++cnt;
    if (cnt < 2) {
        alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
        return false;
    } else {    
        return true;
    }
}
我喜欢麦丽素 2025-01-15 01:03:14

只要您只担心这三个复选框并且不想使用 JavaScript 库,我能想到的最简单的事情就是:

var checkedBoxes = [];

if(theForm.Conservatives.checked) 
    checkedBoxes.push(theForm.Conservatives);
if(theForm.Labour.checked)
    checkedBoxes.push(theForm.Labour);
if(theForm.LiberalDemocrats.checked)
    checkedBoxes.push(theForm.LiberalDemocrats;

// two or more boxes are checked
if(checkedBoxes.length < 2){
    alert('Choose at least two parties.');
}
else {
    // Do stuff with checkedBoxes.
}

此方法不仅会为您提供选中项目的计数,而且还会为您提供已选中项目的计数。如果需要的话,还允许您仅访问代码中稍后的复选框。

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:

var checkedBoxes = [];

if(theForm.Conservatives.checked) 
    checkedBoxes.push(theForm.Conservatives);
if(theForm.Labour.checked)
    checkedBoxes.push(theForm.Labour);
if(theForm.LiberalDemocrats.checked)
    checkedBoxes.push(theForm.LiberalDemocrats;

// two or more boxes are checked
if(checkedBoxes.length < 2){
    alert('Choose at least two parties.');
}
else {
    // Do stuff with checkedBoxes.
}

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.

梅倚清风 2025-01-15 01:03:14

你可以这样做:

if (theForm.Conservatives.checked +
    theForm.Labour.checked +
    theForm.LiberalDemocrats.checked) < 2)
{
 alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
 return false;
} else {    
 return true;
}

You can do:

if (theForm.Conservatives.checked +
    theForm.Labour.checked +
    theForm.LiberalDemocrats.checked) < 2)
{
 alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
 return false;
} else {    
 return true;
}
无需解释 2025-01-15 01:03:14
function checkCheckBoxes(theForm) {
  var opts = ["Conservatives","Labour","LiberalDemocrats"],
      selected = 0;

  for (var i = 0; i < opts.length; i++) {
    if (theForm[opts[i]].checked)
      selected++;
  }
  if (selected < 2) {
    alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
    return false;
  } else {    
    return true;
  }
}
function checkCheckBoxes(theForm) {
  var opts = ["Conservatives","Labour","LiberalDemocrats"],
      selected = 0;

  for (var i = 0; i < opts.length; i++) {
    if (theForm[opts[i]].checked)
      selected++;
  }
  if (selected < 2) {
    alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
    return false;
  } else {    
    return true;
  }
}
无可置疑 2025-01-15 01:03:14
function checkCheckBoxes(theForm) {
 if(theForm.Conservatives.checked + theForm.Labour.checked + theForm.LiberalDemocrats.checked > 1)return true;
 alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
 return false;
}
function checkCheckBoxes(theForm) {
 if(theForm.Conservatives.checked + theForm.Labour.checked + theForm.LiberalDemocrats.checked > 1)return true;
 alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
 return false;
}
烟织青萝梦 2025-01-15 01:03:14
function checkCheckBoxes(theForm) {
    var checkboxes = [theForm.Conservatives, theForm.Labour, theForm.LiberalDemocrats];
    var checked = 0;
    checkboxes.forEach(function(el){ 
                              if (el.checked) checked++;
                       });
    if (checked < 2) 
    {
        alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
        return false;
    } else {    
        return true;
    }
}
function checkCheckBoxes(theForm) {
    var checkboxes = [theForm.Conservatives, theForm.Labour, theForm.LiberalDemocrats];
    var checked = 0;
    checkboxes.forEach(function(el){ 
                              if (el.checked) checked++;
                       });
    if (checked < 2) 
    {
        alert ('Choose At Least Two Parties Who Will Be Standing For This Election');
        return false;
    } else {    
        return true;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文