2011-07-20 26 views
14

Lưu ý: mã bên dưới chỉ để trình diễn và tôi đang sử dụng plugin xác thực jQuery và jQuery.Cách vô hiệu hóa các trường theo cách thủ công sau khi cuộc gọi AJAX với jQuery xác nhận

Giả sử tôi có một mẫu với hai lĩnh vực (email và số hàng tồn kho):

<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 plugin để có dạng:

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 
       */ 
      } 
     }); 
    } 
}); 
+0

Bạn đã bao giờ tìm được giải pháp cho điều này chưa? Đó là chính xác những gì tôi đang cố gắng làm vào lúc này. – Richard

+0

tôi tìm thấy một giải pháp cho điều này chỉ cần cho tôi một thời gian, tôi sẽ cho u biết –

Trả lời

31

Nếu bạn biết rằng những lĩnh vực là không hợp lệ, bạn có thể sử dụng này chức năng. http://docs.jquery.com/Plugins/Validation/Validator/showErrors

var validator = $("#myshowErrors").validate(); 
validator.showErrors({ 
    "firstname": "I know that your firstname is Pete, Pete!" 
}); 

đâu firstname là tên tài sản của lĩnh vực của bạn; tức là name="firstname".

+0

câu trả lời đúng, cảm ơn bạn sir! (OP nên chấp nhận điều này.) –

+16

Điều này sẽ hiển thị lỗi nhưng biểu mẫu vẫn sẽ gửi – Lyon

Các vấn đề liên quan