[英]jquery ajax post selectbox array by name
I want to do an ajax request to update the status of an item, but for one or many selected item. 我想执行ajax请求来更新一项的状态,但是要针对一个或多个选定项。
So how can I post all the selected checkbox items to process it on the handle page? 那么,如何将所有选中的复选框项目过帐以在处理页面上进行处理呢?
Here is some code I use., but it will only post one item
to the process page. 这是我使用的一些代码,但是只会将一个item
到流程页面。
<td>
<input type="checkbox" class="select-all" name="item[]" id="item[78]">
</td>
<td>
<input type="checkbox" class="select-all" name="item[]" id="item[182]">
</td>
And the javascript 和JavaScript
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': "{{ csrf_token() }}}"
}
});
var formData = {
item: $('input[name=item\\[\\]]').val(),
}
var type = "POST";
var my_url = "/posturl";
$.ajax({
type: type,
url: my_url,
data: formData,
success: function (data) {
console.log(formData);
console.log(data);
},
error: function (data) {
console.log('Error:', data);
}
});
form
tag (all input fields in a single form). 将所有复选框都放在form
标签中(所有输入字段都在一个表单中)。 var formData
将数据存储在var formData
Code: 码:
<form id="form-name">
<tr>
<td>
<input type="checkbox" name="item[1]">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="item[32]">
</td>
</tr>
</form>
Javascript: Javascript:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': "{{ csrf_token() }}}"
}
});
var formData = $('#form').serializeArray();
var type = "POST";
var my_url = "/posturl";
$.ajax({
type: type,
url: my_url,
data: formData,
success: function (data) {
console.log(formData);
},
error: function (data) {
console.log('Error:', data);
}
});
This will post an array of items to the url. 这会将一系列项目发布到url。
I did same thing in minor different way. 我以不同的方式做了同样的事情。
I got list of checkbox and names contain their id. 我得到复选框列表,名称包含其ID。
<input type="checkbox" class="bulkChecked" name="{{$value->id}}">
And Inside your event handler 在您的事件处理程序中
var ids = [];
$(".bulkChecked:checked").each(function () {
ids.push($(this).attr("name"));
});
So at this point you have got ids of checked boxes. 因此,在这一点上,您已经获得了复选框的ID。 No you can pass 'ids' array with your ajax call 不,您可以通过ajax调用传递“ ids”数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.