javascript - Make a loop in jquery -
hello want know how make loop js through id because have email id 2 fields 2 different forms able choose when click on id of form right displays error on right form , vice versa
my code :
<script type="text/javascript"> $(document).ready(function(){ $("input#id_st-courriel").focusout(checkemailfield); }); function checkemailfield() { $fieldvalue = $("input#id_st-courriel").val(); $.ajax({ url: '/ajax/checkemailfield', data: ({ value: $fieldvalue }), type: 'get', success: function($data, $textstatus, $xmlhttprequest) { if ($data != '') { $("input#id_st-courriel").parent().prev('errorlist').remove(); $("input#id_st-courriel").parent().before($data); } } }) } </script>
i have id : id_st-courriel , other id second formulaire : id_em-courriel
for moment i're error display on first form id: id_st-courriel
edit :
my views.py
def ajax_check_email_field(request): form = loginform(request.post) html_to_return = '' if 'value' in request.get: field = forms.emailfield() try: field.clean(request.get['value']) except exceptions.validationerror ve: html_to_return = '<ul class="errorlist">' message in ve.messages: html_to_return += '<li>' + message + '</li>' html_to_return += '</ul>' return httpresponse(html_to_return)
you can make minor changes like
- update selector select both fields - either use class selector after add common class both fields or use multiple selector syntax select both elements using id
- in event handler dynamically select current element using
this
reference - in event handlerthis
refer input element triggered event
so
$(document).ready(function() { $("#id_st-courriel, #id_em-courriel").focusout(checkemailfield); //you can simplify selector if can add common class both these fields , use class selector add event handler }); function checkemailfield() { var $fileld = $(this); var $fieldvalue = $fileld.val(); $.ajax({ url: '/ajax/checkemailfield', data: ({ value: $fieldvalue }), type: 'get', success: function($data, $textstatus, $xmlhttprequest) { $fileld.parent().prev('.errorlist').remove();//may .errorlist if class if ($data != '') { $fileld.parent().before($data); } } }) }
Comments
Post a Comment