Post array of multiple checkbox values

前端 未结 4 1234
悲&欢浪女
悲&欢浪女 2020-12-09 06:27

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

JQUERY:

$(\".db\").live(\"change\",          


        
相关标签:
4条回答
  • 2020-12-09 06:52

    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;
    });
    
    0 讨论(0)
  • 2020-12-09 06:57

    $('input[name="mycheckboxes"]:checked').map(function(){ return $(this).val(); }).get().join(",");

    then explode in PHP $mycheckboxes = explode(',',$_GET['mycheckboxes']);

    0 讨论(0)
  • 2020-12-09 07:02

    You need to have the square brackets to specify an array [] on the submitted variable name.

    {'db[]': db}
    
    $(".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;
    });
    
    0 讨论(0)
  • 2020-12-09 07:06
    var checkeditems = $('input:checkbox[name="review[]"]:checked')
                           .map(function() { return $(this).val() })
                           .get()
                           .join(",");
    $.ajax({
        type: "POST",
        url: "/index.php/openItems/",   
        data: "ids=" + checkeditems,                                        
        success: function(msg) { $(".mainContainer").html(msg); }
    });
    
    0 讨论(0)
提交回复
热议问题