【发布时间】:2013-02-15 08:17:04
【问题描述】:
我在使用 Ajax 发送表单时遇到问题。我有一个表单,我想在单击下一步按钮时异步替换为另一个表单。
这是脚本
$(document).ready(function() {
$('#MY_FORM').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(response) {
$('#FORM_DIV').html(response);
}
});
return false;
});
});
form.py
class CountyForm(forms.Form):
county = forms.ModelChoiceField(queryset=County.objects.all(),
empty_label='---Select a county---', required=False)
other = forms.CharField(required=False)
def __init__(self, *args, **kwargs):
super(CountyForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.html5_required = True
self.helper.form_id = 'MY_FORM'
self.helper.add_input(Submit('next', 'Next', css_class='classfinish'))
self.helper.layout = Layout('county','other')
class WardForm(forms.Form):
ward = forms.ModelChoiceField(queryset=Ward.objects.all(),
empty_label='Select a ward')
other = forms.CharField()
def __init__(self, *args, **kwargs):
super(WardForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.html5_required = True
self.helper.add_input(Submit('save', 'Finish'))
self.helper.add_input(Submit('cancel', 'Cancel'))
self.helper.layout = Layout('ward','other')
观看次数
def location(request):
if request.is_ajax() :
wardform = WardForm()
return HttpResponse(wardform)
countyform = CountyForm()
c = {}
c.update(csrf(request))
return render(request,'location.html', {'countyform': countyform})
我希望当我点击县表单中的下一步按钮时,会出现病房表单。
【问题讨论】:
-
你有什么问题?不过,您在
#FORM_DIV之后缺少报价。 -
你的JS有语法错误
-
@Pavel 打错了,谢谢。
-
在您使用的任何浏览器中打开开发人员工具,您应该能够掌握 javascript 错误。以下是有关如何在不同浏览器中查找开发人员工具的详细信息 - computerworld.com/s/article/9225073/…
-
这看起来好像您想升级到表单向导而不是两个表单部分类型的解决方案。 :)