2012-02-22 117 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; 
    } 
}