开发者

How check at lease one checkbox is checked inside ASP.NET repeater control in Javascript

In my asp.net website; i have REPEATER control having child control checklistbox , dynamically generates in code behind.

开发者_如何学运维

I would like to know how can i write javascript function where i have to check at least one checkbox should be checked.

Ho can i acheive? Please Help?


You could wrap the list of checkboxes in a container (e.g. a <div>) with a known ID:

<div id="myCheckboxes">
  <input type="checkbox" ...>
  <!-- ... -->
  <input type="checkbox" ...>
</div>

And then in JavaScript do:

function isOneChecked(containerId) {
  var myDiv = document.getElementByID(containerId);
  if (myDiv != null) {
    var checkBoxes = myDiv.getElementsByTagName("INPUT");

    for (var i=0; i<checkBoxes.length; i++) {
      if (checkBoxes[i].checked == true) {
        return true;
      }
    }
  }
  return false;
}

and call it as:

if (isOneChecked("myCheckboxes")) {
  // whatever
}


The jQuery way:

if($('input[type=checkbox]:checked').length > 0){
  alert('At least one check box is checked')
}

P.S. If it not a problem use frameworks, you can choose form variety of them(jQuery, Prototype, MooTools, Dojo etc.). They will make your life easier and your javascript code more crossbrowser compatible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜