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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在页面底部添加此脚本:
或将
OnClientClick
属性添加到 LinkButton:OnClientClick="if(!$('#form1').valid()) return false;"
Add at the page's bottom this script:
Or add
OnClientClick
property to LinkButton:OnClientClick="if(!$('#form1').valid()) return false;"
提交按钮自然回传到服务器;链接按钮使用 Microsoft 开发的
__doPostBack
技巧回发到服务器。这就是为什么@Yuriy 的解决方案是一个好的解决方案。或者,您可以只使用一个通用方法:并让所有按钮调用您的函数。还有一些其他 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:And have all your buttons call your function. There are a couple of other JQuery/HTML 5 specific ways to handle this.