Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting value from checked checkbox in ajax

Tags:

jquery

ajax

I want to get the values of the checked checkbox here but it is undefined because it becomes an array.. Please help me how to do it.. thank you..

<form>
<input type="checkbox" value="a" name="letter[]" />
<input type="checkbox" value="b" name="letter[]" />
<input type="checkbox" value="c" name="letter[]" />
<input type="button" value="submit"  onclick="rani()" />
</form>
<script>
function rani(){
    $.ajax({
        type:"post",
        url:"ajax.php",
        data:{
            radha:$("[name=letter]").val(),
            },
        success:function(msg){
            alert(msg);
            }
        })
    }
</script>
like image 918
Krishna Avatar asked Dec 26 '22 19:12

Krishna


1 Answers

Here you will get selected checkbox values in a variable checkbox_value and later split by |.

$("#btn_").on('click', function () {
    var checkbox_value = "";
    $(":checkbox").each(function () {
        var ischecked = $(this).is(":checked");
        if (ischecked) {
            checkbox_value += $(this).val() + "|";
        }
    });
    alert(checkbox_value);
    // your awesome code calling ajax
}); 

Sample Demo

like image 151
Satinder singh Avatar answered Dec 28 '22 08:12

Satinder singh