How to advance to the next form input when the current input has a value?

前端 未结 10 1410
傲寒
傲寒 2020-11-30 04:08

I am having a form with lots of entries. I would like to change my focus to the next textbox, once I entered the value in the current textbox. and want to continue this proc

10条回答
  •  醉梦人生
    2020-11-30 04:31

    This should work. Working with and without tabindex.

    var currentlyFocused = undefined;
    var tabableElements = undefined;
    
    /**
     * Compare function for element sort
     * @param {string | null} a
     * @param {string | null} b
     * @param {boolean} asc
     */
    function sortCompare(a, b, asc = true) {
      let result = null;
      if (a == null) result = 1;
      else if (b == null) result = -1;
      else if (parseInt(a) > parseInt(b)) result = 1;
      else if (parseInt(a) < parseInt(b)) result = -1;
      else result = 0;
      return result * (asc ? 1 : -1);
    }
    
    /**
     * When an element is focused assign it to the currentlyFocused variable
     * @param {Element} element
     */
    function registerOnElementFocus(element) {
      element.addEventListener("focus", function(el) {
        currentlyFocused = el.srcElement;
      });
    }
    
    /**
     * Tab Trigger
     */
    function onTabClick() {
      //Select currently focused element
      let currentIndex;
      tabableElements.forEach((el, idx) => {
        //return if no element is focused
        if (!currentlyFocused) return;
        if (currentlyFocused.isEqualNode(el)) {
          //assign current index and return
          currentIndex = idx;
          return;
        }
      });
      //if theres no focused element or the current focused element is last start over
      let lastIndex = tabableElements.length - 1;
      let nextElementidx = currentIndex === undefined || currentIndex == lastIndex ? 0 : currentIndex + 1;
      //Focus
      currentlyFocused = tabableElements[nextElementidx];
      currentlyFocused.focus();
    }
    /**
     * Init must be run after all elements are loadead in the dom
     */
    function init() {
      //Get all tab-able elements
      let nodeList = document.querySelectorAll("input, button, a, area, object, select, textarea, [tabindex]");
      //To array for easier manipulation
      tabableElements = Array.prototype.slice.call(nodeList, 0);
      //Correcting tabindexes to ensure correct order
      tabableElements.forEach((el, idx, list) => {
        let tabindex = el.getAttribute("tabindex");
        //-1 tabindex will not receive focus
        if (tabindex == -1) list.splice(idx, 1);
        //null or 0 tabindex in normal source order
        else if (tabindex == null || tabindex == 0) {
          list[idx].setAttribute("tabindex", 9999 + idx);
        }
      });
      //sort elements by their tabindex in ascending order
      tabableElements.sort((elementA, elementB) => sortCompare(elementA.getAttribute("tabindex"), elementB.getAttribute("tabindex")));
      //register focus event to elements
      tabableElements.forEach(el => registerOnElementFocus(el));
    }
    
    
    
    
      
      
      
    
      
      
    
      Virtual tab
    
    
    
      

    Virtual Tab Demo

    Tab!




    text
    password
    number
    checkbox
    radio





    textarea


    Focus other elements.

提交回复
热议问题