मुझे अजाक्स के साथ एक फॉर्म भेजने में समस्या है। मेरे पास एक ऐसा फॉर्म है जिसे मैं अगले बटन पर क्लिक करके असीमित रूप से किसी अन्य रूप से प्रतिस्थापित करना चाहता हूं।django 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' के बाद उद्धरण खो रहे हैं। –
आपके जेएस में एक वाक्यविन्यास त्रुटि है – Blender
@ पावेल एक टाइपो था, धन्यवाद। – Alexxio