2012-02-22 83 views
0

我需要添加什么以便根据已选中的复选框数来验证?我希望用户在提交数据之前至少选择两个复选框。这里是我的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> 

如有复选框已sleceted或不是当前的javascript代码只验证,但我想它来验证两个复选框。

+0

任何异议,这里使用jQuery?不主张,只是想知道,因为它会简化答案。 – 2012-02-22 01:09:13

回答

2

只是指望有多少检查,看看它是否小于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; 
    } 
} 
+0

THANK U ...它现在可以工作 – 2012-02-22 01:20:19

0

只要你只担心这三个复选框,你不希望使用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. 
} 

这种方法不仅给你检查项目数的计数,但也将让你如果只需要选中的复选框后访问你的代码。

+0

您的意思是“if(checkBoxes.length <2)” – cdm9002 2012-02-22 01:14:43

+0

@ cdm9002 - 是的,是的,我做到了。固定。 – 2012-02-22 01:15:17

0

你可以这样做:

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; 
} 
0
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; 
    } 
} 
0
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; 
} 
0
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; 
    } 
}