你不能在js中运行sql。假设你有一个saveOrder.php和removeOrder.php文件的脚本文件。这些文件应该包含你的sql查询和逻辑,以html字符串的形式返回结果。下面是你如何构建你的javascript和处理你的ajax请求的例子:
$(function() {
var jqxhr = $.ajax({
url: 'saveOrder.php',
type: 'POST',
dataType: 'html', // data type you are expecting to be returned
// data you are passing into your saveOrder.php script that runs your sql
// queries and other logic and returns the result as html
data: {
totalAmount: 100
}
});
// on ajax success
jqxhr.done(function(html){
console.log("success. order has been saved");
// assuming saveOrder.php returns html
// append your list here
$("#someDiv").empty();
$("#someDiv").append(html);
// assuming your list contains a delete button with
// a data attribute of data-id and where data-id value is the id
// sql record id you can do the following
$(".list-item").each(function(index, el) {
var deleteBtn = $(el).find("#delete");
deleteBtn.on('click', function(event) {
event.preventDefault();
// id of the record being deleted
// capatured from:
// <button data-id="25">Delete</button>
var id = $(this).data(id);
// here you can run another ajax call
var jqxhr = $.ajax({
url: 'removeOrder.php',
type: 'POST',
dataType: 'html',
data: {
id: id
}
});
// second ajax successful
jqxhr.done(function(html){
console.log("order removed");
// append the updated list
$("#someDiv").empty();
$("#someDiv").append(html);
});
jqxhr.fail(function(){
console.log("Second Ajax Failed");
});
});
});
});
jqxhr.fail(function(){
console.log("First Ajax Failed");
});
});
第一件事你可以使用'ajax'而不是'jQuery.Load'函数。在'ajax成功'或'ajax complete'之后,你可以轻松实现'reload'功能 –
@sunil如果我实现了,我可以在这个加载的页面中添加删除功能 –
你的意思是你想运行更多'ajax'删除任何记录? –