Programmatically focus on next input field in mobile safari

后端 未结 6 1327
有刺的猬
有刺的猬 2020-11-27 04:26

I have several input fields in line that acts like a crossword answer line:

\"enter

6条回答
  •  再見小時候
    2020-11-27 04:51

    Programmatically moving to the next input field in a mobile browser without dismissing the keyboard appears to be impossible. (This is terrible design, but it's what we have to work with.) However, a clever hack is to swap the input element positions, values, and attributes with Javascript so that it looks like you are moving to the next field when in fact you are still focused on the same element. Here is code for a jQuery plug-in that swaps the id, name, and value. You can adapt it to swap other attributes as necessary. Also be sure to fix up any registered event handlers.

    $.fn.fakeFocusNextInput = function() {
        var sel = this;
        var nextel = sel.next();
        var nextval = nextel.val();
        var nextid = nextel.attr('id');
        var nextname = nextel.attr('name');
        nextel.val(sel.val());
        nextel.attr('id', sel.attr('id'));
        nextel.attr('name', sel.attr('name'));
        sel.val(nextval);
        sel.attr('id', nextid);
        sel.attr('name', nextname);
        // Need to remove nextel and not sel to retain focus on sel
        nextel.remove();
        sel.before(nextel);
        // Could also return 'this' depending on how you you want the
        // plug-in to work
        return nextel;
    };
    

    Demo here: http://jsfiddle.net/EbU6a/194/

提交回复
热议问题