Javascript 如何使用 jQuery 将查询字符串传递给 Ajax 调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7698706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:13:31  来源:igfitidea点击:

How to pass a query string to Ajax call with jQuery?

javascriptjqueryquery-string

提问by Zeynel

This is a follow up to my previous question (unresolved).

这是对我之前的问题(未解决)的跟进。

I fetch itemsfrom the database and display them in a for loop. I use jQuery to hide one of the rows. Now I need to get the main_idof that hidden row and pass it to $.ajax. In the original question Paul suggested to use alert(this.attr("title"));but this line stops the execution of the $.ajaxcall and the call is not executed. When I comment out the alert alert(this.attr("title"));then the ajax call goes through. In that case, I get an error because the display_false()function in the handler does not get the value of the main_id.

items从数据库中获取并在 for 循环中显示它们。我使用 jQuery 来隐藏其中一行。现在我需要获取main_id隐藏行的 并将其传递给$.ajax. 在 Paul 建议使用的原始问题中,alert(this.attr("title"));但此行停止执行$.ajax调用并且未执行调用。当我注释掉警报时alert(this.attr("title"));,ajax 调用就会通过。在这种情况下,我会收到错误消息,因为display_false()处理程序中的函数没有获取main_id.

This is the html of the "hide" link with title=%s.

这是“隐藏”链接的 html title=%s

<a class="false" title=%s href="/useradminpage?main_id=%s&display=false"><span class="small">(hide)</span></a>

So I need to pass the value of the main_idstored in alert(this.attr("title"));to the function display_false()when the ajax call is executed.

所以我需要main_id在执行ajax调用时将存储的值传递alert(this.attr("title"));给函数display_false()

How can I do this?

我怎样才能做到这一点?

The relevant code is below:

相关代码如下:

The Script

剧本

        self.response.out.write("""
<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" /> 
<title>User Admin Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<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("3 - ajax")
        //the following alert stops the ajax call from executing
        alert(this.attr("title"));

        $.ajax({
            url: "/useradminpage?main_id=%s&display=false",
            data: {main_id: "title"}
            success: function(data) {
            display_false()
            alert(4 - "returned");
            }
        });
    });
});


</script>
</head>
<body>
""")

HTML

HTML

#-----------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" title=%s 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, 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")
                    #I tried to get the "title" but this does not work
                    #main_id = self.request.get("title")
                    k = Main.get_by_id(int(main_id))
                    k.display = False                    
                    k.put()

            display_false()    
            ...

Update

更新

Updated the code according to James Montagne's answer (with a few changes). Now, for some reason, document ready is not loading, and the call to hide the row is not working, but ajax call to update the database is working. What am I doing wrong?

根据James Montagne 的回答更新了代码(有一些变化)。现在,由于某种原因,文档就绪未加载,隐藏行的调用不起作用,但更新数据库的 ajax 调用正在起作用。我究竟做错了什么?

<script>

$(document).ready(function() {

    alert("1 - document ready is called")
    $("a.false").click(function(e) {
        $(this).closest("tr.hide").hide("slow");
        var main_id = this.attr("title");
        var display = "false";
        e.preventDefault();
        alert("2 - row is hidden")
    });

    $("a.false").click(function() {
        alert("3 - ajax");

        $.ajax({
            url: "/useradminpage?main_id=%s&display=false",
            data: {main_id: "main_id", display: "display")},
            success: function(data) {
            display_false()
            alert(4 - "returned");
            }
        });
    });
});


</script>

采纳答案by James Montagne

You don't need to alert that value, you need to pass it. Currently you are passing the string title.

你不需要提醒那个值,你需要传递它。目前您正在传递字符串title

    $.ajax({
        url: "/useradminpage?main_id=%s&display=false",
        data: {main_id: this.attr("title")}
        success: function(data) {
        display_false()
        alert(4 - "returned");
        }
    });

回答by Jatin Lathiya

<script>

$(document).ready(function() {

    alert("1 - document ready is called")
    $("a.false").click(function(e) {
        $(this).closest("tr.hide").hide("slow");
        var main_id = this.attr("title");
        var display = "false";
        e.preventDefault();
        alert("2 - row is hidden")
    });

    $("a.false").click(function() {
        alert("3 - ajax");

        $.ajax({
            url: "/useradminpage?main_id=%s&display=false",
            data: {main_id: "main_id", display: "display")},
            success: function(data) {
            display_false()
            alert(4 - "returned");
            }
        });
    });
});


</script>