发布多个复选框值的数组 [英] Post array of multiple checkbox values

查看:120
本文介绍了发布多个复选框值的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么只有一个db复选框值数组的值被发送到服务器端脚本?

Why is only one value of the "db" checkbox values array being sent to the server side script?

JQUERY: $ b

JQUERY:

$(".db").live("change", function() {
    $(this).add($(this).next("label")).add($(this).next().next("br")).remove().insertAfter(".db:last + label + br"); 
    var url = "myurl.php";
    var db = [];
    $.each($('.db:checked'), function() {
        db.push($(this).val()); 
    });
    if(db.length == 0) { 
        db = "none"; 
    }       
    $.post(url, {db: db}, function(response) {
        $("#dbdisplay").html(response); 
    });
    return true;
});

HTML:

HTML:

<input type="checkbox" name="db[]" class="db" value="track"/><label for="track">track</label></br>
<input type="checkbox" name="db[]" class="db" value="gps"/><label for="gps">gps</label></br>
<input type="checkbox" name="db[]" class="db" value="accounting"/><label for="accounting">accounting</label></br>

编辑:我最终回答了自己的问题,文件(或解释)为什么这是必要的?我很难找到确切的答案(因此死后的帖子)。

Edit: I ended up answering my own question, but does anyone have documentation (or an explanation) of why this is necessary? It was difficult for me to find the exact answer (thus the posthumous post).

推荐答案

我同意@jjclarkson。只需添加,而不是将您的ID推送到数组,您可以使用 $ .map

I agree with @jjclarkson. Just to add, instead of pushing your ids to an array, you can use $.map:

$(".db").live("change", function() {
    $(this).add($(this).next("label")).add($(this).next().next("br")).remove().insertAfter(".db:last + label + br"); 
    var url = "myurl.php";

    var db = $('.db:checked').map(function(i,n) {
        return $(n).val();
    }).get(); //get converts it to an array

    if(db.length == 0) { 
        db = "none"; 
    }       
    $.post(url, {'db[]': db}, function(response) {
        $("#dbdisplay").html(response); 
    });
    return true;
});

这篇关于发布多个复选框值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆