4

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

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

2 回答 2

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
}); 

示例演示

于 2013-10-02T07:41:02.623 回答
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);
        }
    })
}
于 2013-10-02T07:22:41.697 回答