admin管理员组

文章数量:1403506

I am using following code to detect whether the check box inside my gridview template field is checked or not. If none of the check box is selected then I want to show alert message.

   function findCheckBox() {

        var inputElements = document.getElementsByTagName('input');
        var chekSelect = false;
        for (var i = 0; i < inputElements.length; i++) {
            var myElement = inputElements[i];


            if (myElement.type === "checkbox") {

                if (myElement.checked === false) {

                    chekSelect = true;
                    return true;
                }
            }

            if (chekSelect === true) {
                return true;
            }

            else {
                alert('Please Check Atleast one record to print cheque!!!');
                return false;
            }
        }  

    }

But with this code when I click on my button its showing me error message for one time even if one or more check box is checked. What I am doing wrong here. Can anyone help me please.

I am using following code to detect whether the check box inside my gridview template field is checked or not. If none of the check box is selected then I want to show alert message.

   function findCheckBox() {

        var inputElements = document.getElementsByTagName('input');
        var chekSelect = false;
        for (var i = 0; i < inputElements.length; i++) {
            var myElement = inputElements[i];


            if (myElement.type === "checkbox") {

                if (myElement.checked === false) {

                    chekSelect = true;
                    return true;
                }
            }

            if (chekSelect === true) {
                return true;
            }

            else {
                alert('Please Check Atleast one record to print cheque!!!');
                return false;
            }
        }  

    }

But with this code when I click on my button its showing me error message for one time even if one or more check box is checked. What I am doing wrong here. Can anyone help me please.

Share Improve this question asked Jul 4, 2013 at 8:06 barsanbarsan 2,49116 gold badges51 silver badges62 bronze badges 1
  • I am so sorry but I dont know Jquery..can you please tell me how can I use it? – barsan Commented Jul 4, 2013 at 8:08
Add a ment  | 

3 Answers 3

Reset to default 3

Your logic is slightly off. Corrected version:

jsFiddle demo

function findCheckBox() {
    var inputElements = document.getElementsByTagName('input');
    var chekSelect = false;
    for (var i = 0; i < inputElements.length; i++) {
        var myElement = inputElements[i];

        if (myElement.type === "checkbox") {
            if (myElement.checked) {
                chekSelect = true;
                break;
            }
        }
    }

    if(!chekSelect) {
        alert('Please Check Atleast one record to print cheque!!!');
        return false;
    } else {
        return true;
    }
}

I've changed the .checked test, to test for it being true not false, because you want to know if at least one checkbox is checked. I also added a break, and moved the alert to outside of the for, because you won't know if there is a checkbox checked until the for pletes.

Try this

function findCheckBox() {
    var inputElements = document.getElementsByTagName('input');
    for (var i = 0; i < inputElements.length; i++) {
        var myElement = inputElements[i];
        if (myElement.type === "checkbox" && myElement.checked) {
            return true;
        }
    }
    alert('Please Check Atleast one record to print cheque!!!');
    return false;
}

Using JQuery:

var checked = false;
$('input:checkbox').each(function(){
    if($(this).prop('checked')){
        checked = true;
        break;
    }
});

if(!checked) alert('Please Check At least one record to print cheque!!!')

本文标签: How to show alert message using javascript to check checkboxStack Overflow