javascript 没有表单需要 html5 输入类型。它有效吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7741123/
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
html5 input type required without a form. Does it work?
提问by Zo72
I want to use: HTML 5 Required.
我想使用:HTML 5 必需。
such as
如
<input type='text' required>
it only seems to work if the input type is in a form. Can I not use it on its own and then by javascript call some sort of validate first method ?
它似乎只有在输入类型是表单时才有效。我不能单独使用它,然后通过 javascript 调用某种验证第一个方法吗?
More comments following feedback:
更多评论如下反馈:
I have
我有
<input id='name' type='text' required>
<input id='surname' type='text' required>
<input id='send' type='button' onclick ='send()'>
function send() {
if (document.getElementById('name').value == '') { alert('missing name'); return }
if (document.getElementById('surname').value == '') { alert('missing surname'); return }
// logic now...
}
The bit where it checks the input params and sends alert that's the bit I would like to change by using HTML 5
它检查输入参数并发送警报的位,这是我想使用 HTML 5 更改的位
采纳答案by RReverser
Sure, use
当然,使用
document.getElementById('your_input_id').validity.valid
to check validity of field dynamically.
动态检查字段的有效性。
回答by YuS
First, the "required" attribute will fire on "submit" event, so, I guess, no form — nothing will be submitted. Second, why there is the problem with form?
首先,“required”属性将在“submit”事件上触发,所以,我猜,没有表单——什么都不会提交。二、为什么会出现形式问题?
回答by Riz
With 'required', this should work only if form is submitted.
使用“必需”,这应该仅在提交表单时才有效。