简体   繁体   English

如何使用javascript遍历html表输入

[英]How to iterate through a html table inputs using javascript

I want to iterator through a table containing input fields without using tab.我想在不使用选项卡的情况下遍历包含输入字段的表。

As tab only iterate through right.由于选项卡仅迭代正确。

I need to use keyboard arrow to iterate through the input fields.我需要使用键盘箭头来遍历输入字段。

<td class="td-pad" *ngFor="let size of sizeRange;">
    <div class="input-group">
        <input type="" class="form-control fs-tr" (change)="changeSelectedQuantity('F',size)" [(ngModel)]="selectedStock[stock]['F'][size]" [disabled]="availableStock[stock]['F'][size]<=0" >
        <div class="input-group-append">
                <span class="input-group-text">/ {{availableStock[stock]['F'][size] ? availableStock[stock]['F'][size] : 0}}</span>
        </div>
    </div>
</td>

I have a below code which we have used in one of our application for the same requirement我有一个下面的代码,我们在我们的一个应用程序中使用了它来满足相同的要求

Using recursion I iterate through the DOM to check if the element is "tr" or "td" and then find the next or previous sibling depending on the key press.使用递归,我遍历 DOM 以检查元素是“tr”还是“td”,然后根据按键找到下一个或上一个同级。 Then get the child input to set the focus.然后获取子输入以设置焦点。

Hope this helps希望这可以帮助

 const NEXT = "next", PREV = "prev", UP = "up", DOWN = "down", TR = "tr", TD = "td"; function arrowPressed(element, type, nName) { var newElement = getInput(element, type, nName); if (newElement != undefined && newElement != null) { if (newElement.disabled) { arrowPressed(newElement, type, nName) } else { newElement.focus(); } } } function getInput(element, type, nName) { if (element != null && element.nodeName.toLowerCase() == nName) { if (element.nextElementSibling != null && (type == NEXT || type == DOWN)) { return element.nextElementSibling.querySelector('.kd-input'); } else if (element.nextElementSibling == null && type == NEXT) { return getInput(element.parentElement, DOWN, TR); } else if (element.previousElementSibling != null && (type == PREV || type == UP)) { return element.previousElementSibling.querySelector('.kd-input'); } else if (element.previousElementSibling == null && type == PREV) { return getInput(element.parentElement, UP, TR); } } else { return getInput(element.parentElement, type, nName); } return null; } document.onkeydown = function(evt) { evt = evt || window.event; if (evt.target && evt.target.className == 'kd-input') { switch (evt.keyCode) { case 37: arrowPressed(evt.target, PREV, TD); break; case 39: arrowPressed(evt.target, NEXT, TD); break; case 38: arrowPressed(evt.target, UP, TR); break; case 40: arrowPressed(evt.target, DOWN, TR); break; } } };
 <table> <tr> <th>Input1</th> <th>Input2</th> <th>Input3</th> <th>Input4</th> <th>Input5</th> </tr> <tr> <td><input class="kd-input"></td> <td><input class="kd-input" disabled></td> <td><input class="kd-input" disabled></td> <td><input class="kd-input"></td> <td><input class="kd-input"></td> </tr> <tr> <td><input class="kd-input"></td> <td><input class="kd-input"></td> <td><input class="kd-input" disabled></td> <td><input class="kd-input"></td> <td><input class="kd-input" disabled></td> </tr> <tr> <td><input class="kd-input" disabled></td> <td><input class="kd-input"></td> <td><input class="kd-input"></td> <td><input class="kd-input"></td> <td><input class="kd-input"></td> </tr> <tr> <td><input class="kd-input"></td> <td><input class="kd-input"></td> <td><input class="kd-input" disabled></td> <td><input class="kd-input" disabled></td> <td><input class="kd-input"></td> </tr> </table>

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

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