简体   繁体   中英

checkbox return only false even it is checked in Jquery

I submit the form while change dropdown option.After submit the form i redirect in to same page. I submit the form Using below function:

function submitform(){
    //Below variables are checkbox value
    var addresscheck = $('#addressmatchedCheck').is(':checked');
    var rcnumbercheck =  $('#rcnumbermatched').is(':checked');
    var phonenumbercheck = $('#phonenumbercheck').is(':checked');
    var idproofcheck = $('#idproofcheck').is(':checked');
    var guarantoraddresscheck = $('#guarantoraddressmatchedCheck').is(':checked');
    var guarantoridcheck = $('#guarantoridproofcheck').is(':checked');
    var guarantorrelationcheck = $('#guarantorrelationshipCheck').is(':checked');
    if( addresscheck &  rcnumbercheck & phonenumbercheck & idproofcheck  & guarantoraddresscheck & guarantoridcheck & guarantorrelationcheck) {
                $('#statusid').val(1);
                alert($('#statusid').val() + "if");
    }
    else{
        $('#statusid').val(2);
        alert($('#statusid').val() + "else");
    }
    $('#fieldVerificationFormID').attr('method', 'POST'); 
    $('#fieldVerificationFormID').attr('action', '/mfi/api/1.0/client/ci/groups/member/fieldverification/insert');
    $('#fieldVerificationFormID').submit();
} 

My problem is checkbox return only false even it is checked? First time ie before form submit it works exactly.Issue is after form submission. can anyone sove my issue?

This is what I use. I have a function that extends JQuery with an isChecked() function. I made this ages ago before the :checked was available in the selectors. But it's pretty bulletproof.

jQuery.fn.isChecked = function() {
    var isChecked = false;
    this.each(function(){
        if (this.checked) {
            isChecked = true;
            return;
        }
    });
    return isChecked;
}

function check() {
    var addressCheck = $('#addressmatchedCheck').isChecked();
}

If you name your checkboxes like this, it works:

<input type="checkbox" id="addressmatchedCheck" checked="checked" />
<input type="checkbox" id="rcnumbermatched" checked="checked" />
<input type="checkbox" id="phonenumbercheck" checked="checked" />
<input type="checkbox" id="idproofcheck" checked="checked" />
<input type="checkbox" id="guarantoraddressmatchedCheck" checked="checked" />
<input type="checkbox" id="guarantoridproofcheck" checked="checked" />
<input type="checkbox" id="guarantorrelationshipCheck" checked="checked" />

Demo: http://jsfiddle.net/Guffa/DqdgC/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM