HTML5表单验证的条件规则 [英] Conditional rules for HTML5 form validation

查看:167
本文介绍了HTML5表单验证的条件规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以根据其他元素状态设置必需元素。例如,假设我有一个输入元素,当且仅当用户检查特定复选框时,我才想要必需。首先,这可以在不使用JavaScript的情况下完成吗?其次,如果我们使用JavaScript,当然可以设置一个规则/验证,如果用户检查该复选框,但是关于默认浏览器验证错误的一致性呢?单击复选框后,将required属性添加到元素是否正确?我在这里看不到替代品。你对这个有什么看法?

I am wondering if it's possible to set an element "required" based on some other element state. For instance, say I have an input element that I want to be "required" if and only if the user checks a specific checkbox. First, is this possible to do it without the use of JavaScript? Second, if we use JavaScript, of course one could set up a rule / validation if the user checks that checkbox, but what about consistency regarding the default browser validation errors? Would it be "correct" to add the "required" attribute to the element once the checkbox is clicked? I don't see an alternative here. What are your thoughts on this one?

推荐答案

我不认为你可以在没有javascript的情况下做到这一点。

I don't think you can do this without javascript.

使用javascript,你可以很容易地做到这一点。

With javascript, you could do this easily enough.

我会添加 required 仅在单击复选框后才归属。

I would add the required attribute only after the checkbox is clicked.

事实上,我会将其设为切换件,这样用户就可以取消选中并取消输入。

In fact, I would make this a toggled piece, so that the user can uncheck and unrequire the input.

但是,我不会依赖浏览器执行所需的属性。相反,我会使用一些CSS创建我自己的必需通知(突出显示,动画,显示一些单词)。

However, I would not rely on browser's implementation of the required attribute. Instead, I would create my own required notification using some CSS (highlighting, animation, displaying some words).

这篇关于HTML5表单验证的条件规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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