jQuery linkbutton错误

时间:2011-12-05 11:15:10

标签: jquery asp.net jquery-validate linkbutton

此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 个答案:

答案 0 :(得分: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;"

答案 1 :(得分:0)

提交按钮自然地回发到服务器; linkbutton使用Microsoft开发的__doPostBack技巧回发到服务器。这就是@ Yuriy的解决方案很好的原因。或者,你可以有一个共同的方法:

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

让所有按钮调用您的功能。还有其他一些JQuery / HTML 5特定方法来处理这个问题。