简体   繁体   English

要求输入文本字段或选择选项(HTML5)

[英]Make either text-INPUT fields or SELECT OPTION required (HTML5)

This is my code so far: 到目前为止,这是我的代码:

https://jsfiddle.net/kfqbbn3k/ https://jsfiddle.net/kfqbbn3k/

There are two divs: 有两个div:

div #1 : div #1

<div class="slidingDiv">
    <div>Firstname</div>
    <div class='resetOptions'>
        <input id='vVorname' name='uVorname' type='text' size='21'></input>
    </div>
    <div style="clear:both;" />
    <div>Lastname</div>
        <div class='resetOptions'>
            <input id='vNachname' name='uNachname' type='text' size='21'></input>
        </div>
        <div style="clear:both;" />
    </div>
</div>

div #2 : div #2

<div class="wrapUser">  <a href="#" class="show_hide">b) Registered Customer</a>

        <div class="slidingDiv">

            <div>
                <select>";
                    <option selected='true' value='' id='navOrderAddOrderChoose'>Choose</option>
                    <option value='CustomerA'>CustomerA</option>
                    <option value='CustomerB'>CustomerB</option>
                </select>
            </div>
            <div style="clear:both;" /></div>
    </div>
    </div>

The 1st div is used if the customer isn't registered in the Database yet. 如果客户尚未在数据库中注册 ,则使用第一个div

The 2nd div is used if the customer is already in the databas e (I removed the PHP Code for this example). 如果客户已经在数据库 e 中,则使用第二个div (在此示例中,我删除了PHP代码)。

What I want to do : I want to add a HTML5 REQUIRED control to these fields. 我想做什么 :我想向这些字段添加HTML5必需控件。 If either the text-INPUT-fields (div1) are filled OR an SELECT OPTION (div2) is selected, the form can submit. 如果填写了文本输入字段(div1)或选择了选择选项(div2),则可以提交表单。 If no div is filled out the form can't pass. 如果未填写div,则表单无法通过。 So: 所以:

REQUIRED is either information from DIV1 or from DIV2. 要求是来自DIV1或来自DIV2的信息。

(Please check the fiddle Code for further information) (请检查小提琴代码以获取更多信息)

Why not initially mark both text fields required then on change event of drop-down either set or remove required attribute for text fields based on the selection made. 为什么不一开始就不标记两个文本字段required然后在下拉的更改事件上根据所选择的内容设置或删除文本字段的required属性。

Here is a fiddle 这是一个小提琴

(points to note: in the markup I've removed novalidate on the input text fields and replaced it with required . Also I've commented the slidingDiv implementation. (注意事项:在标记中,我删除了输入文本字段上的novalidate ,并将其替换为required 。我也评论了slideDiv实现。

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

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