繁体   English   中英

JavaScript推送多维数组仅选中复选框

[英]Javascript push multidimensional array only checked checkboxes

我在页面上有复选框,我只想在更改每个复选框时仅发布检查值

$(function () {
    $('.list input').change(function (e) {
        //e.preventDefault();
        var favorite = [];
        $.each($(".list input[type='checkbox']:checked"), function () {
            favorite[$(this).attr("name")].push = $(this).val();
        });

        var str;
        str = $.param(favorite);

        $.ajax({
            url: '/schema.asp',
            type: 'POST',
            data: str,
            dataType: 'text',
            success: function (response) {
                alert(response);
            }
        });
    });
});

但是我不能做正确的语法来将检查推入数组

$.each($(".list input[type='checkbox']:checked"), function () {
    favorite[$(this).attr("name")].push = $(this).val();
});

请显示正确的方法。

$(this).attr(“ name”)可以与(Make [],Model [],Year())不同,并且必须为字符串

解决了:

由于没有人给出完整答案,所以这是最终的工作代码

    $(function () {
        $('.list input').change(function (e) {
            //e.preventDefault();
            var favorite = {};
            $.each($(".list input[type='checkbox']:checked"), function(){ 
            if(typeof(favorite[$(this).attr("name")]) == 'undefined'){
                favorite[$(this).attr("name")] = [];
            }           
            favorite[$(this).attr("name")].push($(this).val());
        });

            var str;
            str = $.param(favorite);

            $.ajax({
                url: '/schema.asp',
                type: 'POST',
                data: str,
                dataType: 'text',
                success: function (response) {
                    alert(response);
                }
            });
        });
    });

push是要调用的函数, 而不是要设置的属性

var favorite = {};
$.each($(".list input[type='checkbox']:checked"), function(){ 
    if(typeof(favorite[$(this).attr("name")]) == 'undefined'){
        favorite[$(this).attr("name")] = [];
    }           
    favorite[$(this).attr("name")].push($(this).val());
});

另外,请注意,您需要检查对象的属性是否已设置,以便将其初始化为数组。

 $('.list input').change(function(e) { //e.preventDefault(); var favorite = []; $.each($(".list input[type='checkbox']:checked"), function(){ if(typeof(favorite[$(this).attr("name")]) == 'undefined'){ favorite[$(this).attr("name")] = []; } favorite[$(this).attr("name")].push($(this).val()); }); console.log(favorite); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <ul class="list"> <li> <input type="checkbox" name="one" value="1" /> </li> <li> <input type="checkbox" name="two" value="2" /> </li> <li> <input type="checkbox" name="one" value="1" /> </li> <li> <input type="checkbox" name="two" value="2" /> </li> <li> <input type="checkbox" name="one" value="1" /> </li> </ul> 

在jquery中使用map()将数组或对象中的所有项目转换为新的项目数组。

favorite = $(".list input[type='checkbox']:checked").map(function () {
    var obj = {};
    obj[$(this).attr("name")] = this.value;
    return obj;

}).get();

小提琴演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM