Javascript AJAX:向php发布一个值数组
我对javascript/jquery几乎一无所知: 我有一个ajax调用,它返回html表中格式化的条目,如下所示:Javascript AJAX:向php发布一个值数组,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我对javascript/jquery几乎一无所知: 我有一个ajax调用,它返回html表中格式化的条目,如下所示: Stuff | Other stuff | delete stuff ------|----------------|------------------------- value1| from database | delete this entry button ------|----------------|------------------------- v
Stuff | Other stuff | delete stuff
------|----------------|-------------------------
value1| from database | delete this entry button
------|----------------|-------------------------
value2| from database | delete this entry button
上面写着“删除内容”的列有一个按钮,它将调用一个方法来删除该条目。我一直在尝试的是在每一行的末尾添加一个支票簿,并向php发送一个数组来执行多记录删除
电话如下:
$('#menu-notifications').click(function() {
$.ajax({
type: 'GET',
url: '/admin/notifications/ajax_view_notifications/' + <?= $this->session->userdata('user_id'); ?>,
dataType: 'json',
cache: false,
success: function(data) {
$("#notifications-modal .modal-body table").find("tr:gt(0)").remove();
$.each(data, function(i, obj) {
$('#notifications-modal .modal-body table tr:last').after(
'<tr><td>' + htmlDecode(obj.message) + '</td>' +
'<td>' + obj.created_by + '</td>' +
'<td>' + obj.created_dt + '</td>' +
'<td><div class="btn-group">' +
'<a href="/admin/notifications/ajax_delete_notification/' + obj.id + '" class="btn btn-mini delete-notification" id="delete-notification" data-dismiss="modal"><i class="icon-remove"></i></a>' +
// here should be a checkbox to mark for deletion
'</div></td></tr>');
});
}
});
$(“#菜单通知”)。单击(函数(){
$.ajax({
键入:“GET”,
url:'/admin/notifications/ajax_view_notifications/'+,,
数据类型:“json”,
cache:false,
成功:功能(数据){
$(“#notifications modal.modal body table”).find(“tr:gt(0)”).remove();
$。每个(数据、功能(i、obj){
$('#notifications modal.modal body table tr:last')。之后(
''+htmlDecode(对象消息)+''+
“+obj.created_by+”
''+obj.created_dt+''
'' +
'' +
//这里应该有一个复选框来标记删除
'');
});
}
});
我已经成功添加了复选框,但是每次尝试使用
或打开另一个表单都会导致页面根本无法加载(控制台中没有任何内容)
总而言之:我试图在每一行的末尾添加一个复选框,我可以标记这个chexbox,并将每个标记的复选框发送到一个方法。您需要创建每个复选框,它的值将是元素id,然后您需要一个操作(如“全部删除”按钮),通过Ajax将数据发送到PHP(无需表单):
表格{
宽度:100%;
}
th,td{
边框底部:1px实心#ddd;
文本对齐:左对齐;
}
#r{
显示:块;
边框顶部:1px实心#ddd;
填充:10px0;
边缘顶部:10px;
高度:50px;
}
假割台
假割台
假割台
勾选删除
虚构文字
虚构文字
虚构文字
虚构文字
虚构文字
虚构文字
虚构文字
虚构文字
虚构文字
虚构文字
虚构文字
虚构文字
删除所有元素
生成的html是什么样子的,您的表单是否包装了表?目前,根本没有表单。只是一个方法调用。我一直在用这种特殊格式设置表单时遇到问题。至于生成的html,我不确定您的意思。它只是一个由jquery生成的表?太棒了!稍微修改一下,我就明白了你知道我需要什么!
$('#menu-notifications').click(function() {
// Simpler: use getJSON
$.getJSON('/admin/notifications/ajax_view_notifications/' + <?= $this->session->userdata('user_id'); ?>)
// Clearner: use promises :)
.done(function(data) {
$("#notifications-modal .modal-body table").find("tr:gt(0)").remove();
$.each(data, function(i, obj) {
// Create a row
var row = $('<tr>');
// Crete cells
var cells =
'<td>' + htmlDecode(obj.message) + '</td>' +
'<td>' + obj.created_by + '</td>' +
'<td>' + obj.created_dt + '</td>' +
// We use this identified cell to create our delete functionality
'<td class="delete-row"></td>';
// Fill the row
row.append(cells);
// Create the delete button
var deleteButton = $('<a>', {
'href': '/admin/notifications/ajax_delete_notification/' + obj.id
,'class': 'btn btn-mini delete-notification'
// We can't have duplicate ids, so no ids here
// ,id: 'delete-notification'
,'data-dismiss':'modal'
,'html': '<i class="icon-remove"></i>'
});
// Crete the checkbox
var checkbox = $('input', {
'type': 'checkbox'
// We use an HTML array
,'name': 'rowsToDelete[]'
,'value': obj.id
});
// Append the button and the checkbox to the row
// I ignore the .btn-group on purpose
row.find('.delete-row')
.append(deleteButton)
.append(checkbox);
// We add the row to the DOM
$('#notifications-modal .modal-body table tr:last').append(row);
});
});
});
// To delete in group we use a different call, let's say, a 'Delete All' button
$('#delete-all').click(function() {
// We serialize the info of all the buttons
// Find all checkbox under the .delete-row class and serialize it
data = $('.delete-row').find(':checkbox').serialize();
// Send the data to PHP
$.post('/admin/notifications/ajax_delete_notification/delete_a_bunch', data)
// PHP would receive $_POST['rowsToDelete'] = [1, 2, 4, 20]
.done(function(data) {
alert('All selected rows were deleted');
})
.fail(function() {
alert('No rows deleted :/');
});
});