如何在不刷新页面的情况下使用jQuery更新数据库?
如何使用jQuery将此display_false()
函数发送到服务器,以便更新数据库而不刷新页面?
def display_false(): if display == "false": main_id = self.request.get("main_id") k = Main.get_by_id(int(main_id)) k.display = False k.put() display_false()
首先,我用这个jQuery隐藏一个表行( 参见我之前的问题 ):
$(document).ready(function() { $("a.false").click(function(e) { $(this).closest("tr.hide").hide("slow"); e.preventDefault(); });
然后我想用display_false()
将数据库中的“display”属性更新为“false”,这样就不会显示该项。
这是隐藏链接所在的html:
for item in e: main_id = item.key().id() ... ... (hide) ... ...
谢谢!
更新
这是我根据保罗的回答所尝试的 ,但这不起作用。
$(document).ready(function() { //hide the row $("a.false").click(function(e) { $(this).closest("tr.hide").hide("slow"); e.preventDefault(); }); $("a.false").click(function() { //ajax server call $.ajax({ url: "https://stackoverflow.com/useradminpage?main_id=%s&display=false", success: function(data) { //do some stuff. display_false() alert('returned'); } }); }); });
更新
我发出警报,看看保罗建议的运行情况。 警报1,2和3正在运行,但4未运行:
$(document).ready(function() { alert("1 - document ready is called") $("a.false").click(function(e) { $(this).closest("tr.hide").hide("slow"); e.preventDefault(); alert("2 - row is hidden") }); $("a.false").click(function() { //ajax server call alert("3 - ajax server call") $.ajax({ url: "https://stackoverflow.com/useradminpage?main_id=%s&display=false", success: function(data) { //do some stuff. display_false() alert(4 - "returned"); } }); }); });
更新
这是表格该部分代码的一部分; 我想获取main_id
并将其传递给ajax调用:
#-----------main table------------# main_id = self.request.get("main_id") self.response.out.write(""" links edit tags """) query = Main.all() query.filter("owner", user) query.filter("display", True) query.order("-date") cursor = self.request.get("cursor") if cursor: query.with_cursor(cursor) e = query.fetch(100) cursor = query.cursor() for item in e: main_id = item.key().id() self.response.out.write(""" https://stackoverflow.com/questions/7696540/how-to-update-database-with-jquery-without-refreshing-the-page/%s (https://stackoverflow.com/questions/7696540/how-to-update-database-with-jquery-without-refreshing-the-page/%s)
https://stackoverflow.com/questions/7696540/how-to-update-database-with-jquery-without-refreshing-the-page/%s (edit) (hide) (comments) https://stackoverflow.com/questions/7696540/how-to-update-database-with-jquery-without-refreshing-the-page/%s """ % tuple([item.url, item.title, urlparse(item.url).netloc, f1.truncate_at_space(item.pitch), main_id, main_id, main_id, item.url, main_id, (", ".join(item.tag_list)), (", ".join(item.tag_list)),])) self.response.out.write("""
""") display = self.request.get("display") def display_false(): if display == "false": main_id = self.request.get("main_id") k = Main.get_by_id(int(main_id)) k.display = False k.put() display_false()
与Paul讨论后更新以获取隐藏行的ID号:
$(document).ready(function() { alert("1 - document ready is called") $("a.false").click(function(e) { $(this).closest("tr.hide").hide("slow"); e.preventDefault(); alert("2 - row is hidden") }); $("a.false").click(function() { alert(this.attr("title")); $.ajax({ url: "https://stackoverflow.com/useradminpage?main_id=%s&display=false", success: function(data) { display_false() alert(4 - "returned"); } }); }); });
您无法使用JQuery更新服务器数据库。 您所能做的就是发送由服务器处理的请求。
使用JQuery.Ajax或该函数的任何衍生产品来发送请求。 对于您的服务器,它将看起来像一个常规请求。
在包含jQuery库之后,你需要一个AJAX调用。
$(document).ready(function() { //false click action $("a.false").click(function () { //ajax server call $.ajax({ url: '/useradminpage?main_id=%s&display=false', success: function(data) { //do some stuff. alert('returned'); } }); }); });