简体   繁体   English

没有表单需要html5输入类型。它有用吗?

[英]html5 input type required without a form. Does it work?

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发送我想要改变的警报

Sure, use 当然,使用

document.getElementById('your_input_id').validity.valid

to check validity of field dynamically. 动态检查字段的有效性。

First, the "required" attribute will fire on "submit" event, so, I guess, no form — nothing will be submitted. 首先,“required”属性将触发“submit”事件,因此,我猜,没有形式 - 什么都不会提交。 Second, why there is the problem with form? 第二,为什么表格有问题?

使用'required'时,只有在提交表单时才能使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM