从ajax中的复选框中获取值

时间:2013-10-02 07:19:30

标签: jquery ajax

我想在这里获取复选复选框的值,但它是未定义的,因为它变成了一个数组..请帮我怎么做..谢谢..

<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>

2 个答案:

答案 0 :(得分:10)

在这里,您将获得变量checkbox_value中的选定复选框值,然后按|分割。

$("#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

<强> Learn jQuery: Simple and easy jQuery tutorials blog

答案 1 :(得分:0)

尝试

function rani() {
    var letters = $('input[name="letter[]"]:checked').map(function(){
        return this.value;
    }).get()
    $.ajax({
        type: "post",
        url: "ajax.php",
        data: {
            radha: letters,
        },
        success: function (msg) {
            alert(msg);
        }
    })
}

否则尝试

function rani() {
    $.ajax({
        type: "post",
        url: "ajax.php",
        data: $('form').serialize(),
        success: function (msg) {
            alert(msg);
        }
    })
}