微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 发布多个复选框值的数组

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

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:

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

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

我同意@jjclarkson.只是添加,而不是将您的ID推送到数组,您可以使用 $.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;
});

原文地址:https://www.jb51.cc/php/139056.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐