【问题标题】:submit a form with jQuery function使用 jQuery 函数提交表单
【发布时间】:2012-09-16 05:25:08
【问题描述】:

我有一个 html 页面,其中包含一个表单,我希望当表单成功提交时,显示以下 div:

<div class="response" style="display: none;">
  <p>you can download it<a href="{{ link }}">here</a></p>
</div>

我还有一个jquery函数:

    <script type="text/javascript">
        $(function() {
            $('#sendButton').click(function(e) {            
                e.preventDefault();
                var temp = $("#backupSubmit").serialize();
                validateForm();
                $.ajax({
                    type: "POST",
                    data: temp,
                    url: 'backup/',
                    success: function(data) {
                        $(".response").show();
                    }
                });
            });
        });

</script>

在我的views.py(代码后面)中,我创建了一个链接并将其传递给 html 页面。我有:

def backup(request):
    if request.is_ajax():
        if request.method=='POST':
            //create a link that user can download a file from it. (link)
            variables = RequestContext(request,{'link':link})
            return render_to_response('backup.html',variables)
        else:
            return render_to_response('backup.html')
    else:
        return render_to_response("show.html", {
            'str': "bad Request! :(",
            }, context_instance=RequestContext(request))
backup = login_required(backup)

我的问题:我的视图似乎没有执行。它没有显示我发送到此页面的链接。似乎只执行了 jQuery 函数。我很困惑。我怎样才能让它们都执行(我的意思是 jQuery 函数,然后是我在这个函数中设置的 url,它使我的视图被执行。)

我不知道如何使用序列化功能。每当我搜索时,他们写道:

.serialize() 方法以标准 URL 编码表示法创建一个文本字符串,并生成类似“a=1&b=2&c=3&d=4&e=5”的查询字符串。

我不知道什么时候必须使用它,而我可以在 request.Post["field name"] 中访问我的表单字段。而且我不知道在我的情况下成功的数据应该是什么:函数(数据)。

非常感谢您的帮助。

【问题讨论】:

    标签: javascript jquery html ajax django


    【解决方案1】:

    您必须从您的 ajax 发布函数中获取并显示 data,其中 data 是您通过 DJango 服务器呈现的响应,例如:

    t = Template("{{ link }}")
    c = Context({"link": link})
    t.render(c):
    

    你的 JS / jQuery 应该变成这样:

    <script type="text/javascript">
        $(function() {
            $('#sendButton').click(function(e) {            
                e.preventDefault();
                var temp = $("#backupSubmit").serialize();
                validateForm();
                $.ajax({
                    type: "POST",
                    data: temp,
                    url: 'backup/',
    
                    success: function(data) {
                        // 'data' is the response from your server
                        // (=the link you want to generate from the server)
    
                        // Append the resulting link 'data' to your DIV '.response'
                        $(".response").html('<p>you can download it<a href="'+data+'">here</a></p>');
    
                        $(".response").show();
                    }
                });
            });
        });
    </script>
    

    希望这会有所帮助。

    【讨论】:

    • wait mate.. 我对你的回答有点困惑...... XD 事情是:当你提交你点击你的sendButton,你会在views.py中收到ajax请求,通过你的功能。所以,在这个函数中,你必须生成你的链接并渲染它(没有模板:只需“打印”链接)。渲染后,此渲染信息(=您的链接)将作为 data(您的成功函数中的变量)发送。然后,您将这个新生成的链接添加到您的 DIV 中,然后就可以了。我不确定我是否清楚:S...如果我错了,或者这不是你真正想要的,请告诉我。
    • 非常感谢。正如您所说,数据是来自服务器的响应,我从views.py 收到。 "t = Template("{{ link }}") c = Context({"link": link}) t.render(c):" 是什么意思?我通过 render_to_response 将链接数据发送到我的 html 页面,我的问题是我不知道在 html 页面的哪里接收它?你能告诉我在哪里可以在 html 中创建数据(使用链接)吗?如果数据应该是从视图接收的“链接数据”,那么“数据:临时”是什么?我为什么要使用“数据:临时”?总是需要使用序列化?对不起,这是我第一次使用这个功能:">谢谢你的帮助:)
    • aha,你的意思是每当我将字典传递给我的模板(html 文件),(在我的例子中是链接),参数“数据”自动包含链接,如果我发送到字典(链接和其他),“数据”包含两者?
    • 通常data 自动包含服务器“打印”的答案。所以,它只能是一个字符串。
    • 我认为您只需要在应该响应 ajax 请求的 django 函数中呈现/“打印”您的链接。
    猜你喜欢
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    相关资源
    最近更新 更多