Html html5 所需的验证器不适用于输入类型 = 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17163023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:13:01  来源:igfitidea点击:

html5 required validator not working with input type=button

htmlrequiredrequired-field

提问by iJade

Here is my html code

这是我的 html 代码

<form id="form1" runat="server">
<input id="q" required />
<input id="btn" type="submit" value="Search">
</form>

I have used html5 required field validators, it works but with a post back. so modified the code as follows to avoid postback

我已经使用了 html5 必需的字段验证器,它可以工作但带有回发。所以修改代码如下以避免回发

<form id="form1" runat="server">
<input id="q" required />
<input id="btn" type="button" value="Search">
</form>

But the required validator doesn't work

但是所需的验证器不起作用

回答by Max

That's because the required validator is only called on submit, and the type=button is not a submit. Try this (http://jsfiddle.net/upgradellc/vrTLw/):

这是因为所需的验证器仅在提交时调用,而 type=button 不是提交。试试这个(http://jsfiddle.net/upgradellc/vrTLw/):

<form id="form1" runat="server">
    <input id="q" required />
    <input id="btn" type="submit" value="Search">
</form>