gpt4 book ai didi

jQuery 验证适用于输入类型 ="submit",但不适用于 html 按钮元素。为什么?

转载 作者:行者123 更新时间:2023-12-01 01:02:56 25 4
gpt4 key购买 nike

使用 ASP.NET,为什么 bassistance.de jQuery 验证插件在使用 input type="submit"元素而不是 html 按钮元素时阻止表单提交?

使用 html 按钮 (type="submit") 标记时会触发验证,但表单仍会提交。

有没有办法让 jQuery 验证插件与 html 按钮元素一起工作?

一个简单的例子:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
$(document).ready(function () {
$("#form1").validate({
rules: {
txtFirstName: {
required: true
}
},
messages: {
txtFirstName: {
required: "* First Name required<br/>"
}
}
});
});
</script>
</head>

<body>
<form id="form1" runat="server">
<input id="txtFirstName" name="txtFirstName" type="text" />
<input id="btnSubmit1" type="submit" runat="server" value="Submit" /> <!-- WORKS! -->
<button it="btnSubmit2" type="submit" runat="server">Submit</button> <!-- DOESN'T WORK -->
</form>
</body>
</html>

最佳答案

它似乎是这样设计的:

// when a submitHandler is used, capture the submitting button
if (validator.settings.submitHandler) {
inputsAndButtons.filter(":submit").click(function () {
validator.submitButton = this;
});
}

不幸的是,这似乎已经成为现实,没有其他选择。 :select 上的文档似乎对此提供了更多说明:

The :submit selector typically applies to button or input elements. Note that some browsers treat element as type="default" implicitly while others (such as Internet Explorer) do not.

一种解决方案是将您的按钮绑定(bind)到调用验证的函数。在下面的示例中,我们看到这是通过 anchor 完成的:

$("#myform").validate();
$("a.check").click(function() {
$("#myform").valid();
});

关于jQuery 验证适用于输入类型 ="submit",但不适用于 html 按钮元素。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10666890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com