简体   繁体   English

复选框即使在Jquery中选中也仅返回false

[英]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? 我的问题是,即使被选中,复选框也仅返回false? 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. 我有一个使用isChecked()函数扩展JQuery的函数。 I made this ages ago before the :checked was available in the selectors. 我在:checked在选择器中可用之前就做了这个年龄。 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/ 演示: http//jsfiddle.net/Guffa/DqdgC/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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