jeudi 27 juillet 2017

how to get checked values from a checkbox-group using jquery

I want to get the values of all the checked checkboxes in the checkbox-group.

{"type":"checkbox-group","label":"Checkbox Group","name":"checkbox-subjects","values":[
{"label":"physics","value":"phy"},
{"label":"chemistry","value":"chemistry"},
{"label":"math","value":"math"}
]},

Following is script code

    document.getElementById('btn').addEventListener('click', function() {

        $(document).ready(function(){

        var x = $(".userform").find(":input");
        $.each(x, function(i, field){
            if(field.type == "text")
            {
                $("#results").append("name:   "+field.name + ":       " +"Value:    "+ field.value +"<br>");
            }

            else if(field.type == "checkbox")
            {
             var result = $('input[type="checkbox"]:checked');

             if(result.length > 0)
                {
                $("#results").append("this is checked     "+"name:   "+field.name + ":       " +"Value:    "+ field.value +"<br>");
            }
            else{

                $("#results").append("this is unchecked");
            }
            }


        });

});


});

When I leave all uncheck then it gives this output

    this is unchecked 
    this is unchecked 
    this is unchecked

but when I check any it gives this output

this is checked name: checkbox-group-1500619332922: Value: on
this is checked name: checkbox-group-1500619332922: Value: on
this is checked name: checkbox-group-1500619332922: Value: on

Thanks in Advance




Aucun commentaire:

Enregistrer un commentaire