如何通过Ajax的使用jQuery发送复选框数组的值?
我有很多表单字段(12 XN行)的一种形式。每行中的第一场(从而重新presents的产品)是类似于此的复选框:
I have a form with a lot of form fields (12 x n rows). The first field in each row (which represents a product) is a checkbox that resembles this:
<input type="checkbox" class="ids" name="ids[]" value="1">
每个复选框的值是唯一的。
The value of each checkbox is unique.
我所要做的就是通过Ajax发送选定值的PHP脚本进行处理。我上午有有越来越的ID到服务器正确的问题是什么。我已经使用几件事,包括尝试:
What I am trying to do is send checked values to a PHP script for processing via Ajax. What I am am having issues with is getting the IDs to the server properly. I have tried using several things including:
$('.ids:checked').serialize();
和
var ids = [];
$('.ids:checked').each(function(i, e) {
ids.push($(this).val());
});
$.ajax({
url: "stub",
type: "post",
dataType: "json",
data: {
'ids[]': 'ids[]='+ids.join('&ids[]=')
},
success: function(data) {
// stub
}
});
但这些既造成服务器上得到这样的:
But these both result in getting this on the server:
ids[]=104&ids;[]=105
我可以序列的整体形式,它送过来,但是这可能会导致大量的数据被发送的将是未使用的。
I could serialize the whole form and send it over but that could result in a lot of data being sent that is going to be unused.
我如何发送只值delete []
到服务器?理想情况下的方式,PHP将其识别为一个数组?
How do I send only the values of delete[]
to the server? Ideally in a way that PHP recognizes it as an array?
(我身边有它的工作通过在发送ID作为一个逗号分隔的字符串,但想知道如何做到这一点,因为我花了足够的时间,试图弄明白)。
(I have worked around it by sending the IDs over as a comma delimited string but would like to know how to accomplish this since I spent enough time trying to figure it out).
这工作得很好,我
<input type="checkbox" class="ids" name="ids[]" value="2">
<input type="checkbox" class="ids" name="ids[]" value="3">
<input type="checkbox" class="ids" name="ids[]" value="4">
<input type="checkbox" class="ids" name="ids[]" value="5">
<input type="checkbox" class="ids" name="ids[]" value="6">
<div id="response"></div>
<button id="submit">Submit</button>
<script>
$('#submit').click(function() {
$.ajax({
url: "stub.php",
type: "post",
data: $('.ids:checked').serialize(),
success: function(data) {
$('#response').html(data);
}
});
});
</script>
然后在stub.php
Then on stub.php
var_dump($_POST);