0

我正在使用 jquery .load() 将 django 模板加载到弹出 div 中

我使用的模板有一个表单,当我加载模板时,表单不再有效,但是当我通过 url 访问模板时,表单是有效的。

谁能想到在 jquery 加载后表单不再有效的任何原因?

这是我的表格:

def createrecipe(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/index/')
    else:
        if request.method == 'POST':
            form = RecipeForm(request.POST)
            if form.is_valid():
                recipe = form.save(commit=False)
                recipe.original_cookbook = request.user.cookbooks.all()[0]
                recipe.pub_date = datetime.datetime.now()
                recipe.save()
                user = request.user
                cookbooks = user.cookbooks
                cookbook = cookbooks.all()[0]
                cookbook.recipes.add(recipe)
                return HttpResponseRedirect('/account')
        else:
            form = RecipeForm()

        return render_to_response('cookbook/createrecipe.html',
                                    {'form':form},
                              context_instance=RequestContext(request))

这是我的表单模板:

{% block content %}
<div id="createform">
<h1>Create New Recipe</h1>
    <form action="." method="POST">
        <table>
            {% csrf_token %}
            {{ form.as_table }}
        </table>
        <p><input type="submit" value="Submit"></p>
    </form>
</div>
{% endblock %}

这是加载函数:

$(document).ready(function(){
    $(".create").on("click", function(){
        $("#popupContact").load("/cookbook/createrecipe #createform");
    });
});

谢谢你,

零食鱼

4

1 回答 1

0

这是您通过 jQuery 或默认表单行为执行的提交按钮,我认为您的代码看不到加载的表单,您应该在处理 ajax 数据时使用 jquery 的 .on() 方法。http://api.jquery.com/on/

于 2012-04-22T23:34:53.390 回答