jQuery 链接按钮错误

发布于 2024-12-19 11:21:47 字数 1999 浏览 2 评论 0原文

这个 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>

This jquery function working fine for button submit but not working for link button. Why?

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱要勇敢去追 2024-12-26 11:21:47

在页面底部添加此脚本:

<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;"

Add at the page's bottom this script:

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

Or add OnClientClick property to LinkButton: OnClientClick="if(!$('#form1').valid()) return false;"

病女 2024-12-26 11:21:47

提交按钮自然回传到服务器;链接按钮使用 Microsoft 开发的 __doPostBack 技巧回发到服务器。这就是为什么@Yuriy 的解决方案是一个好的解决方案。或者,您可以只使用一个通用方法:

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

并让所有按钮调用您的函数。还有一些其他 JQuery/HTML 5 特定方法可以处理此问题。

The submit button posts back to the server naturally; the linkbutton posts back to the server using a __doPostBack trick that Microsoft developed. That's why @Yuriy's solution is a good one. Alternatively, you could just have a common method:

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

And have all your buttons call your function. There are a couple of other JQuery/HTML 5 specific ways to handle this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文