Javascript 多复选框组值的多维数组jQuery post数组AJAX

Javascript 多复选框组值的多维数组jQuery post数组AJAX,javascript,jquery,Javascript,Jquery,我有以下代码。我想向数组添加/删除复选框值,并传递给JavaScript ajax调用。我希望能够为ajax调用将所有排序数组传递给on数组。每次更改复选框时,它都会更新数组,而数组又会更新传递给ajax的数组 <input class='sort1' type='checkbox' value='' name='sort1' /> <input class='sort1' type='checkbox' value='' name='sort1' /> <inpu

我有以下代码。我想向数组添加/删除复选框值,并传递给JavaScript ajax调用。我希望能够为ajax调用将所有排序数组传递给on数组。每次更改复选框时,它都会更新数组,而数组又会更新传递给ajax的数组

<input class='sort1' type='checkbox' value='' name='sort1' />
<input class='sort1' type='checkbox' value='' name='sort1' />
<input class='sort1' type='checkbox' value='' name='sort1' />

<input class='sort2' type='checkbox' value='' name='sort2' />
<input class='sort2' type='checkbox' value='' name='sort2' />

<input class='sort3' type='checkbox' value='' name='sort3' />
<input class='sort3' type='checkbox' value='' name='sort3' />
<input class='sort3' type='checkbox' value='' name='sort3' />
<input class='sort3' type='checkbox' value='' name='sort3' />


var arr_sort = {}
var arr_sort1 = {};
var arr_sort2 = {};
var arr_sort3 = {};

    $(".sort1").delegate("input[type='checkbox']", 'change', function() {
        var $this = $(this);

        if ($this.prop('checked')) {
            arr_sort1[$this.attr('name')] = $this.val();
        } else {
            delete arr_sort1[$this.attr('name')];
        }

        ajax_call();

    });

$(".sort2").delegate("input[type='checkbox']", 'change', function() {
        var $this = $(this);

        if ($this.prop('checked')) {
            arr_sort2[$this.attr('name')] = $this.val();
        } else {
            delete arr_sort2[$this.attr('name')];
        }

        ajax_call();

    });

$(".sort3").delegate("input[type='checkbox']", 'change', function() {
        var $this = $(this);

        if ($this.prop('checked')) {
            arr_sort3[$this.attr('name')] = $this.val();
        } else {
            delete arr_sort3[$this.attr('name')];
        }

        ajax_call();

    });

function ajax_call(){

    $.ajax({
         type: 'POST',
         url: "file.php",
         data: { thisaction: thisaction, sort: arr_sort},
         success: function(data){ 
             $("#results").html(data);
            }
        }); 

}
回答:

注意事项:

  • 每个
    名称
    都必须是唯一的。
    无论如何,这是一种很好的形式。我把它们命名为box,但你应该使用更合适的名称来描述它们是什么
  • 使用
    数据
    属性对对象进行分组。
    这是语义方法
  • 您的
    输入
    应以
    形式
    标记,并带有
    id
    这有助于您使用jQuery选择器获取所需的对象
  • 我删除了
    属性。
    如果愿意,您可以重新输入值。我只是不希望看到其中的value属性
  • 您应该将所有状态存储在一个对象或数组中。这样您就可以在一个位置管理所有选中的状态
回答:

注意事项:

  • 每个
    名称
    都必须是唯一的。
    无论如何,这是一种很好的形式。我把它们命名为box,但你应该使用更合适的名称来描述它们是什么
  • 使用
    数据
    属性对对象进行分组。
    这是语义方法
  • 您的
    输入
    应以
    形式
    标记,并带有
    id
    这有助于您使用jQuery选择器获取所需的对象
  • 我删除了
    属性。
    如果愿意,您可以重新输入值。我只是不希望看到其中的value属性
  • 您应该将所有状态存储在一个对象或数组中。这样您就可以在一个位置管理所有选中的状态

你能把问题说得更具体些吗?我不明白你在问什么。你能把问题说得更具体些吗?我不明白你在问什么。@nnnnnn,你需要知道delete不会从数组中删除元素,它只会将其设置为未定义,所以你可以得到类似于
group1:{box1:undefined,box2:undefined,box3:undefined}
获取此错误$this.prop不是函数[Break On this error],如果($this.prop('checked')){.prop()仅在jQuery1中引入。6@atma在现代浏览器中测试,该属性已被删除。我不确定完整的浏览器范围是多少。@madphp除非您有理由不升级,否则您确实应该升级jQuery版本。@nnnnnn,您需要知道delete不会从数组中删除元素,它只会将其设置为未定义,因此您可以得到类似于
group1:{box1:undefined,box2:undefined,box3:undefined}
get this.prop不是函数[Break On this error],如果($this.prop('checked')){.prop()仅在jQuery1中引入。6@atma在现代浏览器中测试,该属性已被删除。我不确定完整的浏览器范围是多少。@madphp除非你有理由不升级,否则你真的应该升级jQuery版本。
array (
   "sort1" => array("1","2","3"),
   "sort2" => array("this","that"),
   "sort3" => array("other","ok")
)

or

array (
   "sort1" => array("1","2","3"),
   "sort2" => array("this")
   "sort3" => array()
)