如何在不刷新页面的情况下使用jQuery更新数据库? [英] How to update database with jQuery without refreshing the page?

查看:65
本文介绍了如何在不刷新页面的情况下使用jQuery更新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery将此 display_false()函数发送到服务器,以便在不刷新页面的情况下更新数据库?

How do I send this display_false() function to the server with jQuery so that the database is updated without refreshing the page?

    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隐藏一个表行(见上一个问题):

First I hide a table row with this jQuery (see my previous question):

$(document).ready(function() {

    $("a.false").click(function(e) {
    $(this).closest("tr.hide").hide("slow");
    e.preventDefault();

});

然后我想用 display_false()将数据库中的display属性更新为false,这样该项目就不会显示。

and then I want to update the "display" property in the database to "false" with display_false()so that the item is not displayed.

这是隐藏链接所在的html:

And this is the html where the hide link is:

    for item in e:
        main_id = item.key().id()
        ...
        <tr class="hide">
        ...
        <a class="false" href="/useradminpage?main_id=%s&display=false"><span class="small">(hide)</span></a>
        ...
        </td>
        </tr>
        ...

谢谢!

更新

这是我根据 Paul的答案,但这不起作用。

This is what I tried according to Paul's answer, but this is not working.

$(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: "/useradminpage?main_id=%s&display=false",
    success: function(data) {
    //do some stuff.
    display_false()
    alert('returned');
  }
});
});


});

更新

我发出警报,看看保罗建议的运行情况。警报1,2和3正在运行但4未运行:

I put alerts to see what is running as suggested by Paul. Alerts 1, 2 and 3 are running but 4 is not running:

$(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: "/useradminpage?main_id=%s&display=false",
    success: function(data) {
    //do some stuff.
    display_false()
    alert(4 - "returned");
  }
});
});


});

更新

这是表格该部分代码的一部分;我想获取 main_id 并将其传递给ajax调用:

This is the part of the code for that section of the table; I am trying to get the main_id and pass it to ajax call:

#-----------main table------------#

            main_id = self.request.get("main_id")

            self.response.out.write("""<table class="mytable">
            <tr class="head">
            <th  width="80%">links</th><th>edit tags</th>
            </tr>    
            """)        

            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("""
                <tr class="hide">
                <td><a href="%s" target="_blank">%s</a><span class=small> (%s) </span><br />
                <span class=small>%s</span>
                <a href="/edit?main_id=%s"><span class="small">(edit)</span></a>
                <a class="false" href="/useradminpage?main_id=%s&display=false"><span class="small">(hide)</span></a>
                <a href="/comment?main_id=%s"><span class="small">(comments)</span></a></td>
                <td><a href="/tc?url=%s&main_id=%s&user_tag_list=%s" title="edit tags">%s</a>
                </td>
                </tr>
                """ % 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("""</tbody></table>""")    

            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号:

<script>

$(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: "/useradminpage?main_id=%s&display=false",

    success: function(data) {
    display_false()
    alert(4 - "returned");
  }
});
});
});


</script>


推荐答案

您无法使用JQuery更新服务器数据库。您所能做的就是发送由服务器处理的请求。

You cannot update a server database with JQuery. All you can do is send a request that is handled by the server.

使用JQuery.Ajax或该函数的任何副产品来发送请求。对于您的服务器,它将看起来像常规请求。

Use JQuery.Ajax or any spin-offs of that function to send the request. To your server it will look as a regular request.

这篇关于如何在不刷新页面的情况下使用jQuery更新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆