Uwaga: poniższy kod służy tylko do demonstracji i używam jQuery i jQuery validate plugin.Jak ręcznie unieważnić pola po wywołaniu AJAX z jQuery validate
Załóżmy, że mam formularz z dwóch pól (e-mail i numer inwentarzowy):
<form action="/something" method="post" id="demoForm">
Inventory No: <input type="text" class="required" name="inventory_no" />
Email: <input type="text" class="required" name="email" />
<input type='submit' value='submit' />
</form>
Binding wtyczki do postaci:
jQuery(function(){
jQuery('#demoForm').validate();
});
//handle user submit
jQuery('#demoForm').bind('submit', function (e) {
e.preventDefault();
if (jQuery(e.target).valid()) {
//form is valid submit request thru ajax
jQuery.ajax({
/*more options here*/
success: function (data) {
/*
Server process request and finds that, email is already in use
and inventory number is invalid
so it sends data in some format may pe JSon
with field names and message indicating errors
eg: Email:"Already in use"
InventoryNo: "Invalid Inventory No",
Now can i invalidate these two fields on form mannualy
and display the message received from server for each field
*/
}
});
}
});
Czy kiedykolwiek znalazłeś rozwiązanie tego problemu? Dokładnie to właśnie próbuję zrobić w tej chwili. – Richard
znalazłem rozwiązanie tego po prostu daj mi trochę czasu, dam ci znać –