Hi, I have a two part form which I have developed. One is for existing members and one is not.

On one of the forms I have a checkbox which will need to be checked before they can proceed, to determine that they are on the correct form. I will use the radio button which hides and unhides forms.

However when I try to get a handle on if the radio button is checked and then checking to see if the checkbox is checked I get nothing returned.

Can someone help me implement this? My JS is below or view my JSFIDDLE:

$(document).ready(function(){

    var check;   

    if($("input:radio[name=existingpartner]").is(":checked")){

        // Example 3 - With jQuery prop  
    $("#test-with-prop").on("click", function(){
        check = $("#mycheckbox").prop("checked");
        if(check) {
             alert("Checkbox is checked.");
        } else {
            alert("Checkbox is unchecked.");
        }
    }); 
});
有帮助吗?

解决方案

In your if statement you are checking if the element is checked, but in the DOM the element is not checked therefore it will not bind the event on the button.

Move the if statement inside the bind event.

$("#test-with-prop").on("click", function () {
    if ($("input:radio[name=existingpartner]").is(":checked")) {
        check = $("#mycheckbox").prop("checked");
        if (check) {
            alert("Checkbox is checked.");
        } else {
            alert("Checkbox is unchecked.");
        }
    }
}); 

DEMO

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top