2010-06-28 85 views
0

我已經設置了驗證。淨形式與jQuery插件上,一切正常,在頁面加載,但回發確認後停止工作。jQuery驗證插件。後經過驗證工作不回來

然後,如果我空需要選用一個領域,試圖提交,當我的.Net驗證抓住問題的客戶端,然後在字段中的現場驗證再次開始工作。

這裏是重現該問題小的代碼示例:

<script language="javascript"> 
    $(document).ready(function() { 
     ValidateElements(); 
    }); 

    function ValidateElements() { 
      jQuery.validator.messages.required = " "; 
      jQuery.validator.messages.email = " "; 

      var rules = {}; 
      rules[$("#<%=TxtInvoiceName.ClientID%>").attr("name")] = { 
       required: true 

      }; 

      $('form').validate({ 
       rules: rules, 
       success: function (label) { 
       }, 
       errorPlacement: function (label) { 

       } 

      }); 

      $("#MainForm").validate().form(); 
     } 
</script> 

<style> 
    input.error { 
    border: 1px solid red; 
    } 
</style> 

<form id="MainForm" runat="server"> 
<div> 
    <asp:TextBox runat="server" ID="TxtInvoiceName" Text="" /> 
       <asp:RequiredFieldValidator ID="RequiredFieldInvoiceName" runat="server" ErrorMessage="" 
        Display="Dynamic" ControlToValidate="TxtInvoiceName"></asp:RequiredFieldValidator> 
       <asp:Label runat="server" ID="LblTxtInvoiceNameValidate" AssociatedControlID="TxtInvoiceName">&nbsp;&nbsp;&nbsp;</asp:Label> 
       <asp:Button runat="server" Text="PostBack" OnClick="PostBack" /> 
</div> 
</form> 

希望有人能指出我什麼即時做錯了。

回答

0

我已經修改了你的ValidateElements運作了一下,試試這個:

 function ValidateElements() { 
       jQuery.validator.messages.required = " "; 
       jQuery.validator.messages.email = " "; 

       var rules = {}; 
       rules[$("#<%=TxtInvoiceName.ClientID%>").attr("name")] = { 
        required: true 

       }; 

       var $form = $('#MainForm'); 
       $form.validate({ 
        rules: rules, 
        success: function (label) { 
        }, 
        errorPlacement: function (label) { 

        } 

       }); 

       // whenever an input element's blur event fires, 
       // revalidate the form. do this because the asp.net 
       // validators use this behavior 
       $form.find(':input').blur(function() { 
        $form.valid(); 
       }); 

        // revalidate the form on submit. 
        // better approach because it requires less form revalidation 
        // on the client side, but it won't be in "synch" with the 
        // asp.net client side validation 
//    $('#<%= Button1.ClientID %>').click(function() { 
//     return $form.valid(); 
//    }); 
      } 
1

您只需要添加您的網頁上一個小腳本回發

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); 
function EndRequestHandler(sender, args) { 
    ValidateElements(); 
} 
後,以取代所有的驗證規則

這將放回您的驗證規則在接下來的形式提交。