2011-12-05 126 views
1

此jquery函數適用於按鈕提交,但不適用於鏈接按鈕。爲什麼?jQuery鏈接按鈕錯誤

<html> 
<head> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#form1").validate({ 
       rules: { 
        <%= txtUserName.UniqueID %>: {minlength: 5, required: true}, 
        <%= txtPassword.UniqueID %>: {minlength: 5, required: true}, 
        <%= txtEmail.UniqueID %>: {required: true}, 
        <%= txtURL.UniqueID %>: {required: true}, 
        <%= chkbox.UniqueID%>: {required:true}, 
        <%= textcredit.UniqueID %>:{required:true}, 
       }, 
       messages: { 
        <%= txtUserName.UniqueID %>: { 
         required: "Plaese enter your name", 
         minlength: "User name must be atleaet of 5 characters" 
        }, 
        <%= txtPassword.UniqueID %>: { 
         required: "Plaese enter your password", 
         minlength: "Password must be atleaet of 5 characters" 
        }, 
        <%= txtEmail.UniqueID %>:{ required: "Plaese enter your Email Id",}, 
        <%= txtURL.UniqueID %>:{ required: "Plaese enter Website URL",}, 
        <%= chkbox.UniqueID %>:{ required: "Plaese select this chek box",} , 
        <%= chkbox.UniqueID %>:{  
         required: "Plaese select creditcard", 
         minlength: "User name must be atleaet of 5 characters" 
        }, 
       } 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <table width="50%" cellpadding="2" cellspacing="4" style="border: solid 1px navy; background-color: #d5d5d5;"> 
     </table> 
    </form> 
</body> 
+2

如果您提供更多代碼,它可能會更有幫助。例如,究竟是服務器代碼輸出?這些值可能對jQuery沒有意義。 –

+0

可能重複的http://stackoverflow.com/questions/649762/asp-net-linkbutton-imagebutton-and-jquery-validate – goodeye

回答

0

提交按鈕回發到自然的服務器; linkbutton使用微軟開發的__doPostBack技巧回發到服務器。這就是爲什麼@尤里的解決方案是一個很好的解決方案。或者,你可以只是有一個共同的方法:

function validateForm(id) { 
    return $("#" + id).valid(); 
} 

並讓所有的按鈕調用你的功能。有幾個其他的JQuery/HTML 5特定的方法來處理這個問題。

5

添加在頁面的底部這個腳本:

<script type="text/javascript"> 
    var originalDoPostBack = __doPostBack; 
    __doPostBack = function (sender, args) { 
      if ($("#form1").valid() === true) { 
       originalDoPostBack(sender, args); 
      } 
    } 
</script> 

或者添加OnClientClick屬性的LinkBut​​ton:OnClientClick="if(!$('#form1').valid()) return false;"

+0

工作..謝謝! –