【问题标题】:jquery, ajax: several post-requests instead of onejquery, ajax: 多个 post-requests 而不是一个
【发布时间】:2013-05-15 21:54:58
【问题描述】:

我正在使用 django 和 jQuery,我尝试在提交后更改按钮视图而不重新加载页面。当我第一次按下它时一切正常,但第二次按下导致两个 POST 请求,第三次到三个等等,按钮不再改变。尽管数据库中的数据会随着每次按下按钮而发生变化。

请帮我找出我的 jquery 代码有什么问题以及如何防止发布请求乘法

 function send()
{
    $('.valid').submit(function() 
        {
            var a_id = $(this).attr('a_id');
            var vl = $(this).attr('vl');
            $.ajax(
            { 
                        data: {'a_id' : a_id},
                        type: 'POST', 
                        url: 'vl/', 
                        success: function(data, status) 
                        { 
                            $('#mark_'+a_id).html('<i class="icon-white icon-ok"></i> Correct!')
                            $('#mark_'+a_id).attr("id",'cor_'+a_id);        

                            $('#cor_'+a_id).html('Not correct')
                            $('#cor_'+a_id).attr("id",'mark_'+a_id);

                        },
                        error: function(xhr, data) 
                        {
                            alert('fail: ' + data +' answer #'+ a_id);
                        }           
            });

        });

}

a_id 用于回答 id,它来自模板中的按钮

{% if answer.validity %}

<button id="cor_{{ answer.id }}" class="btn btn-inverse js-mark " a_id="{{answer.id}}"  onclick = "send()" ><i class="icon-white icon-ok"></i> Correct answer</button>

{% else %} 

<button id="mark_{{ answer.id }}" class="btn btn-inverse js-mark" a_id="{{answer.id}}" onclick = "send()" >Mark this answer as correct</button>

{% endif %}

views.py 部分:

def question_vl(request, question_id):
    context_instance=RequestContext(request)
    question = get_object_or_404(Question, pk=question_id)

    if request.user == question.author:
        if request.method == 'POST':
            if(request.POST.get('a_id')): 
                        a = Answer.objects.get(pk=request.POST.get('a_id'))
                        if a.validity==False:
                    a.validity = True
                else:
                    a.validity = False
                a.save()
        return HttpResponse("")

【问题讨论】:

    标签: jquery ajax django-templates


    【解决方案1】:

    这一行

    $('.valid').submit(function()
    

    将提交事件附加到表单。因此,每当您按下按钮功能“发送”时,都会再次附加相同的事件。

    修改你的按钮:

    {% if answer.validity %}
    
    <button class="btn btn-inverse js-mark" a_id="{{answer.id}}" onclick = "send(this)" ><i class="icon-white icon-ok"></i> Correct answer</button>
    
    {% else %} 
    
    <button class="btn btn-inverse js-mark" a_id="{{answer.id}}" onclick = "send(this)" >Mark this answer as correct</button>
    
    {% endif %}
    

    这样修改你的函数:

    function send(button)
    {
            button = $(button);
            var a_id = button.attr('a_id');
            $.ajax(
            { 
                        data: {'a_id' : a_id},
                        type: 'POST', 
                        url: 'vl/', 
                        success: function(data, status) 
                        { 
                            if (data.validity){
                               button.html('<i class="icon-white icon-ok"></i> Correct!')
                            } else {
                               button.html('Not correct!')
                            }     
                        },
                        error: function(xhr, data) 
                        {
                            alert('fail: ' + data +' answer #'+ a_id);
                        }           
            });
    
    }
    

    您的服务器端脚本应返回具有“有效性”实际值的 JSON 数据

    【讨论】:

    • 非常感谢!!这确实可以防止乘法。但它引发了其他一些问题:现在 a_id 通常是页面上的第一个按钮,所以无论我按什么按钮,只有第一个按钮会发生变化。你不知道是什么问题吗?
    • 不清楚你想要实现什么。页面上有多少个按钮和表单?如果您有多个表单,您似乎每次都提交第一个表单。但我只能猜测。
    • 我在问题下方有答案列表,每个答案都有一个用于将答案标记为正确的按钮。我希望能够按几次它,这样它就会像“正确”-“不正确”-“正确”一样改变。所以我尝试将按钮 id 指定为 "cor_"+a_id 和 "mark_"+a_id 并每次都更改它们,但现在似乎这些根本不起作用,我想知道是否有正确的方法来更改按钮外观
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2015-06-11
    • 2014-10-14
    • 2017-03-09
    相关资源
    最近更新 更多