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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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;
});

脚本宝典总结

以上是脚本宝典为你收集整理的php – 发布多个复选框值的数组全部内容,希望文章能够帮你解决php – 发布多个复选框值的数组所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。