简体   繁体   English

JavaScript:查找表单输入的第一个子项是输入框还是选择下拉列表

[英]JavaScript: Finding whether first child of a form input is an input box or select dropdown

I am trying to write a script on a form that searches within the HTML for a DD tag. 我正在尝试在HTML格式内搜索DD标记的表单上编写脚本。 When it finds the tag I want to detect whether the first child contained in the DD tag is an input box or select box and if it to set the focus to it. 当找到标签时我想检测DD标签中包含的第一个子项是输入框还是选择框,以及是否将焦点设置为它。 There could potentially be more than DD tag on one page so I am only concerned with the first instance of it. 在一个页面上可能有多个DD标记,所以我只关心它的第一个实例。

Essentially, what I am trying to do is when the page loads, it focuses on the first form field on the page so that the user can enter data directly without the need to click on a box or dropdown. 本质上,我想要做的是当页面加载时,它关注页面上的第一个表单字段,以便用户可以直接输入数据而无需单击框或下拉列表。 I need he logic to be written in pure JS (so not JQuery solutions please) and to accommodate if a given page might have an input box as the first field or a select box as the first field. 我需要他的逻辑用纯JS编写(所以请不要使用JQuery解决方案)并且如果给定页面可能有一个输入框作为第一个字段或选择框作为第一个字段。

JSFiddle: http://jsfiddle.net/3UqSq/ JSFiddle: http//jsfiddle.net/3UqSq/

Any help would be great! 任何帮助都会很棒!

The HTML for input box: 输入框的HTML:

  <div class="container">
     <form class="myform">
       <fieldset>
          <dl>
            <dt><label for="input1">Label 1</label></dt>
            <dd>
               <input id="input1" type="text" value=""/>
               <div>This is some extra DIV's for copy</div>
               <div>This is some extra DIV's for copy</div>
               <div>This is some extra DIV's for copy</div>
            </dd>
            <dt><label for="input2">Label 2</label></dt>
            <dd>
               <input id="input2" type="text" value=""/>
               <div>This is some extra DIV's for copy</div>
               <div>This is some extra DIV's for copy</div>
               <div>This is some extra DIV's for copy</div>
            </dd>
         </dl>
       </fieldset>
     </form>
   </div>

The HTML for select box: 选择框的HTML:

  <div class="container">
     <form class="myform">
       <fieldset>
          <dl>
            <dt><label for="input1">Label 1</label></dt>
            <dd>
               <select id="input1">
                 <option>Option 1</option>
                 <option>Option 2</option>
                 <option>Option 3</option>
               </select>
               <div>This is some extra DIV's for copy</div>
               <div>This is some extra DIV's for copy</div>
               <div>This is some extra DIV's for copy</div>
            </dd>
            <dt><label for="input2">Label 2</label></dt>
            <dd>
               <input id="input2" type="text" value=""/>
               <div>This is some extra DIV's for copy</div>
               <div>This is some extra DIV's for copy</div>
               <div>This is some extra DIV's for copy</div>
            </dd>
         </dl>
       </fieldset>
     </form>
   </div>

Include this in a script at the bottom of your page or after load: 将其包含在页面底部的脚本中或加载后:

​var dd = document.getElementsByTagName('dd')[0];

if (dd.children[0].tagName.toLowerCase() == 'select' ||
    dd.children[0].tagName.toLowerCase() == 'input') {
    dd.children[0].focus();
}

See demo 见演示

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

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