jQuery验证适用于输入类型= [提交"但是不是HTML按钮元素。为什么? [英] jQuery validation works with input type="submit", but not html button element. Why?
问题描述
使用ASP.NET,那为什么使用输入型时bassistance.de jQuery的验证插件prevents表单提交=提交元素,而不是HTML按钮元素?
使用HTML按钮(TYPE =提交)标记时,但形式仍然提交的确认火灾。
有没有一种方法,使的HTML按钮元素jQuery的验证插件的工作?
一个简单的例子:
< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$(#Form1的)。验证({
规则:{
txtFirstName:{
要求:真
}
},
消息:{
txtFirstName:{
要求:*名字需要< BR />中
}
}
});
});
< / SCRIPT>
< /头> <身体GT;
<表ID =form1的=服务器>
<输入ID =txtFirstNameNAME =txtFirstName类型=文本/>
<输入ID =btnSubmit1类型=提交=服务器值=提交/> <! - 务! - >
<按钮,它=btnSubmit2类型=提交=服务器>提交< /按钮> <! - 并不奏效 - >
< /表及GT;
< /身体GT;
< / HTML>
这似乎是这样设计的:
//当使用submitHandler,捕捉提交按钮
如果(validator.settings.submitHandler){
inputsAndButtons.filter(:提交)点击(函数(){。
validator.submitButton =这一点;
});
}
不幸的是,这似乎是烤,没有其他选择。在的文档:选择
似乎摆脱这方面的一些更多的光线:
的
:提交
选择通常适用于按钮或输入元素。请注意,一些浏览器处理元素键入=默认
含蓄而其他(如Internet Explorer)没有。
块引用>一个解决办法是你的
按钮
绑定到调用验证功能。在下面,我们看到这是有固定完成例如:$(#的MyForm)验证()。
$(答。查看)。点击(函数(){
$(#MyForm的)有效();
});Using ASP.NET, why is it that the bassistance.de jQuery validation plugin prevents form submission when using input type="submit" elements, and not html button elements?
The validation fires when using an html button (type="submit") tag, but the form is still submitted.
Is there a way to make the jQuery validation plugin work with html button elements?
A quick example:
<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>
解决方案It appears to be designed that way:
// when a submitHandler is used, capture the submitting button if (validator.settings.submitHandler) { inputsAndButtons.filter(":submit").click(function () { validator.submitButton = this; }); }
Unfortunately that seems to be baked in, with no other option. The documentation on
:select
seems to shed some more light on this:The
:submit
selector typically applies to button or input elements. Note that some browsers treat element astype="default"
implicitly while others (such as Internet Explorer) do not.One solution would be to bind your
button
to a function that invokes the validation. In the example below we see this being done with an anchor:$("#myform").validate(); $("a.check").click(function() { $("#myform").valid(); });
这篇关于jQuery验证适用于输入类型= [提交&QUOT;但是不是HTML按钮元素。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!