我正在使用此代码,它对我有用。如您所见,我没有在表单中添加任何操作。我在提交表单时使用 jquery 'on' 函数,因为所有表单都在 div#creation 中重新加载和更改。那么 ajax url 必须是显示表单的那个。
在我的例子中,当我点击某个按钮时,表单的第一步也是通过带有 get 的 ajax 呈现的。这就是为什么起初 div 中没有任何形式的原因。 (我正在使用引导程序的模态)。
<div id="creation">
<!-- form to be display through ajax -->
</div>
View 中 FormWizard 类中重新加载的模板如下 html:
template_name = 'creation_form.html'
由 creation_form.html 提供的代码:
<form id="creation-form" action="#" method="post">
{% csrf_token %}
<table>
{{ wizard.management_form }}
{{ wizard.form }}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" class="btn btn-primary" aria- hidden="true" type="submit" value="{{ wizard.steps.first}}">First</button>
<button name="wizard_goto_step" class="btn btn-primary" aria-hidden="true" type="submit" value="{{ wizard.steps.prev}}">Previous</button>
{% endif %}
<input id="create-submit" class="btn btn-primary" type="submit" value="submit" />
</form>
这是我的 ajax 调用:
$('#creation').on('submit', '#creation-form' , function(e){
e.preventDefault();
var fd = new FormData($('#creation-form').get(0));
$.ajax({
url: '/create/',
data: fd,
type: "POST",
success: function(data){
$('#creation').html(data);
},
processData: false,
contentType: false
});
});
希望这是对您的回答的正确回应。
我目前很难进入第一步/上一步,如果你弄明白了,请告诉我怎么做。
这就是你要找的东西?