如何不验证具有必需属性的 HTML5 文本框 [英] How not to validate HTML5 text box with required attribute

查看:38
本文介绍了如何不验证具有必需属性的 HTML5 文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<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.

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

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

推荐答案

试试这个;

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

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

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.

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

这篇关于如何不验证具有必需属性的 HTML5 文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆