最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I make sure that at least one checkbox is checked? - Stack Overflow

programmeradmin0浏览0评论

I am using checkboxes whose values is coming from database. Its name is same but name is fetching like:

<input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>">
<%=bs.getServiceDesc()%>

through this i am getting the values from the database. Now i have to validate that at least one checkbox should be selected..

If any one can help me i shall be thankful to u. If i am giving like this the javascript code:

var services = document.getElementsById( 'chkBankServices' );
if(!(services[0].checked) && 
   !(services[1].checked) && 
   !(services[2].checked) && 
   !(services[3].checked) && 
   !(services[4].checked) && 
   !(services[5].checked) && 
   !(services[6].checked) &&
   !(services[7].checked) && 
   !(services[8].checked))
{ 
    alert( "Please select at least one service to submit." );
    return false;
}

It's not giving any alert message. Is anything wrong in that. Plz help me... Thanks in advance

I am using checkboxes whose values is coming from database. Its name is same but name is fetching like:

<input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>">
<%=bs.getServiceDesc()%>

through this i am getting the values from the database. Now i have to validate that at least one checkbox should be selected..

If any one can help me i shall be thankful to u. If i am giving like this the javascript code:

var services = document.getElementsById( 'chkBankServices' );
if(!(services[0].checked) && 
   !(services[1].checked) && 
   !(services[2].checked) && 
   !(services[3].checked) && 
   !(services[4].checked) && 
   !(services[5].checked) && 
   !(services[6].checked) &&
   !(services[7].checked) && 
   !(services[8].checked))
{ 
    alert( "Please select at least one service to submit." );
    return false;
}

It's not giving any alert message. Is anything wrong in that. Plz help me... Thanks in advance

Share Improve this question edited May 8, 2009 at 7:54 Bjorn 71.9k40 gold badges140 silver badges164 bronze badges asked May 8, 2009 at 7:49 remishremish 1
  • Didn't mean to overwrite anyone else's edits. Just happened at the same time. – Bjorn Commented May 8, 2009 at 7:55
Add a comment  | 

6 Answers 6

Reset to default 8

in jQuery :

   alert(  $("#chkBankServices input[type=checkbox]:checked").length > 0 );

Try this:

var services = document.getElementById( 'chkBankServices' );
var checkboxes = services.getElementsByTagName('input');
var checked = false;
for (var i=0,i0=checkboxes.length;i<i0;i++)
if (checkboxes[i].type.toLowerCase()=="checkbox")
{
    if (checkboxes[i].checked) checked = true;
}

and then:

if (!checked)
{
    alert('Not checked');
    return false;
}

There is no getElementsById method, since there should only be one element with a given id. Perhaps you meant to use getElementsByName? This allows multiple elements to be returned.

As this is really a client side issue, it would help if you could post a sample of the generated HTML, and we can guide you further.

Have you checked the rendered source to make sure your checkboxes are being given the expected names?

I don't know if this will help you with your specific problem, but your code would be easier to read if you avoided the massive if block and used a loop instead:

var checked = false;
for(var i=0;i<services.length;i++){
  if(services[i].checked){
    checked = true;
  }
}
if(!checked){
    alert( "Please select at least one service to submit." );
    return false;
}

Looking at your code, I'm betting you have that checkbox in a repeater of some sort and are creating multiple checkboxes with the same ID which is invalid html. I would wrap it in a div/span or something with an id like below:

 if (!isSomethingChecked()) {
    alert( "Please select at least one service to submit." );
    return false;
 }

  function isSomethingChecked() {
    var parent = document.getElementById("chkBankServices");
    for (var child in parent.childNodes) {
        var node = parent.childNodes[child];
        if (node && node.tagName === "INPUT" && node.checked) {
            return true;
        }
    }
    return false;
 }

I assumed the HTML looks like :

<div id="chkBankServices">
    <input type="checkbox" id="Checkbox1" />
    <input type="checkbox" id="Checkbox2" checked="checked"/>
    <input type="checkbox" id="Checkbox3" checked="checked"/>
    <input type="checkbox" id="Checkbox4" />
    <input type="checkbox" id="Checkbox5" />
    <input type="checkbox" id="Checkbox6" />
    <input type="checkbox" id="Checkbox7" />
</div>
发布评论

评论列表(0)

  1. 暂无评论