【发布时间】:2023-03-18 18:58:01
【问题描述】:
我有一个包含两列的表:一列名称和一列复选框。
我有一个相应的 SQL 数据库,其中包含一个 id 列、name 列和一列用于复选框。
我已经让网页自动提交选中/取消选中并根据每个框是否选中或未选中来修改 SQL 数据库,但是如果不重新加载整个页面,我无法弄清楚如何做到这一点。
我对 AJAX 不是很熟悉,但认为这可能是我需要的。谁能指导我寻找好的资源?
谢谢!
【问题讨论】:
我有一个包含两列的表:一列名称和一列复选框。
我有一个相应的 SQL 数据库,其中包含一个 id 列、name 列和一列用于复选框。
我已经让网页自动提交选中/取消选中并根据每个框是否选中或未选中来修改 SQL 数据库,但是如果不重新加载整个页面,我无法弄清楚如何做到这一点。
我对 AJAX 不是很熟悉,但认为这可能是我需要的。谁能指导我寻找好的资源?
谢谢!
【问题讨论】:
是的,这应该通过$.ajax() 完成。当然,请使用jQuery library。
$('form').submit(function(e){
e.preventDefault(); // the page will no longer refresh.
var that = $(this); //cache a reference to the form before our $.ajax
$.ajax({
type: that.prop('method'), //use the method of the form for the ajax call (post/get)
url: that.prop('action'), //use the action of the form as the url for the ajax call
data: that.serialize(), //send the form elements and their respective values
success: function(data){
//if your script returns any data it will be held within the "data" variable
console.log(data); //log data to the console
}
});
});
【讨论】: