2014-03-05 35 views
-1

我想使用jquery插件validate()方法來驗證我當前的sharepoint visual webpart中的div。 我不知道爲什麼這不起作用。它什麼也沒做。使用jQuery驗證插件在asp.net控件不工作

這是代碼。

 <div id="main" runat="server"> 
      <h3>2. Select your study subject.<span class="red">*</span></h3>        
         <asp:RadioButtonList CssClass="required" ID="rdb_study_popul" runat="server" OnSelectedIndexChanged="rdb_study_popul_SelectedIndexChanged"> 
          <asp:ListItem>Individuals</asp:ListItem> 
          <asp:ListItem>Population</asp:ListItem> 
         </asp:RadioButtonList> 
     </div> 

      <asp:Button ID="btn_studysubject_section" runat="server" CssClass="WBSButtonhide" OnClick="btn_studysubject_section_Click" 
         Text="Next"/> 

這裏是jQuery的

 $("input[type='submit']").click(function() { 
     if ($(this).val() != 'Back') { 
      var names = []; 
      var info = " "; 
      $('#<%= main.ClientID %>').validate({ 
      rules: 
        {     
        <%= rdb_study_popul.ClientID%> : { required: true } 
        },    
      messages: 
        { 
        <%= rdb_study_popul.ClientID%> : "This field cannot be empty, please enter between" 
        } 
      }); 

     } 
    }); 

回答

1

它不工作,因爲你引用一個對象,它不是你的頁面上存在。由於您的div標籤上有runat="server",因此該標籤的Id不是「主」。

<div id="main" runat="server"> 

你需要做到這一點在你的jQuery:

$('#<%= main.ClientID %>').validate({.... 
+0

編輯但仍無法正常工作。在驗證函數中它說錯誤的語法缺少一個{我不知道它在哪裏 – user2664298

+1

當我將jQuery事件添加到asp.net服務器控件時,我發現只需向控件添加一個類, 。我會添加一個名爲類似jqMainDiv的類(表示它僅用作jquery鉤子),然後將其用作您的選擇器。我會爲你引用的其他兩個ID做同樣的事情。 – str8killinit

+0

感謝您的想法我是jquery的新手你能告訴我如何編寫規則,如果有附加的css類。我不知道.... – user2664298