我有以下代碼。我想向數組添加/刪除複選框值並傳遞給JavaScript ajax調用。我希望能夠將所有排序數組傳遞給ajax調用的數組。每次更改一個複選框時,它都會更新數組,這又會更新傳遞給ajax的數組。多維複選框組值的多維數組jQuery post數組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);
}
});
}
我也許可以做到這一點,但我想通過數組PHP來處理,特別是如果有更多的複選框組添加。
成果
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()
)
你能否提出更具體的問題?我不明白你在問什麼。 – nnnnnn