Html 如何不验证具有必需属性的 HTML5 文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18202784/
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 12:05:27  来源:igfitidea点击:

How not to validate HTML5 text box with required attribute

htmlbuttonrequired

提问by abramlimpin

I have one HTML text box (for quantity) and two HTML buttons (Add, Cancel) inside my form.

我的表单中有一个 HTML 文本框(用于数量)和两个 HTML 按钮(添加、取消)。

<form>
    <input type="number" min="1" max="100" required />
    <button type="button">Add</button>
    <button type="button">Cancel</button>
</form>

I do not want my second button (cancel) to validate the form when clicked.

我不希望我的第二个按钮(取消)在单击时验证表单。

Is this possible? In ASP.NET, I can use CausesValidation="false" to not trigger the validation.

这可能吗?在 ASP.NET 中,我可以使用 CausesValidation="false" 来不触发验证。

回答by Abhishek Jain

Try this;

尝试这个;

<button type="button" formnovalidate>Cancel</button>

I changed your code: What you want is that your form should not be validated on click of cancel, so that's why i added the formnovalidate to the button cancel.

我改变了你的代码:你想要的是你的表单不应该在点击取消时被验证,所以这就是我将 formnovalidate 添加到按钮取消的原因。

<form>
    <input type="number" min="1" max="100" required />
    <input type="submit">Add</input>
    <input type="submit" formnovalidate>Cancel</input>
</form>

回答by Pouki

Try to add formnovalidateattribute to your cancel button

尝试为formnovalidate您的取消按钮添加属性

<button type="button" formnovalidate>Cancel</button>

回答by proditor

You could use the formnovalidate-option on the cancel-button, like this:

您可以在取消按钮上使用 formnovalidate-option,如下所示:

<input name="cancel" type="submit" value="Cancel" formnovalidate/>

See Symfony2 form validation with html5 and CANCEL button

使用 html5 和 CANCEL 按钮查看Symfony2 表单验证

回答by Bhavin Mistry

If you have the input button as runat=serverthen you must have the property causesvalidation="false"or else if you are simply using html input type button for cancel use javascriptto redirect to your page on click of cancel.

如果您有输入按钮,runat=server那么您必须拥有该属性causesvalidation="false",否则,如果您只是使用 html 输入类型按钮进行取消javascript,则在单击取消时重定向到您的页面。

For eg. 1)

例如。1)

<input type="button" id="btnCancel" class="button blue" value="Cancel" **causesvalidation="false"** onserverclick="btnCancel_Click" runat="server" />

2)

2)

<input type="button" id="btnCancel" class="button blue" value="Cancel" **onclick="GoTo();"**/>

<script type="text/javascript">
function GoTo() {
            window.location.href = '../default.aspx';
        }
</script>

回答by alduar

Try this code at asp.net 5enter link description herebutton control

在 asp.net 5 中尝试此代码在此处输入链接描述按钮控件

<form>
<input type="number" min="1" max="100" required />
<button type="button">Add</button>
<button type="button" formnovalidate="formnovalidate">Cancel</button>

Aldo Flores Reyes @alduar .net news

奥尔多·弗洛雷斯·雷耶斯 @alduar .net 新闻

回答by HUSEN JAMBUGOHDA

try this one.it works fine.It will work on submit button

试试这个。它工作正常。它可以在提交按钮上工作

<asp:TextBox runat="server" ID="inputusername" required />