Looping through Checkboxes using Javascript

后端 未结 3 1052
说谎
说谎 2021-01-27 12:35

I tried using javascript samples that I found here and other places. The problem is that I am using a Table control working at the server, so the javascript I was using does not

相关标签:
3条回答
  • 2021-01-27 13:01

    You need something like this

    var pass = true;
    
    for (var i = 0; i < form.elements.length; i++ ) 
    {
        if (form.elements[i].type == 'checkbox')
        {
            if (form.elements[i].checked == false)
            {
                pass = false;
            }
        }
    }
    
    if(!pass)
    {
        alert ('You must check all the checkboxes!');
    }
    
    return pass;
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-27 13:11

    You can get the HTML ID of the checkboxes by using the C# ClientID property. Insert that ID into your Javascript, and you will then be able to select the relevant checkboxes and do whatever you like with them.

    0 讨论(0)
  • 2021-01-27 13:14

    Use querySelectorAll to check for any unchecked check boxes. If there are any, throw the error message, else postback.

    function checkCheckboxes(){
       if(document.querySelectorAll('input[type="checkbox"]:not(:checked)').length > 0){
         alert("all checkboxes must be checked");
         return false;
       } else{
         return true;
       }
    }
    

    Note: this will only work in Modern browsers Firefox 3.1+, IE8+ (only in IE8 standards mode), and Safari 3.1+

    0 讨论(0)
提交回复
热议问题