简体   繁体   English

jquery 设置 tabindex 和 cursor

[英]jquery set tabindex and cursor

I have the following code that assigns tabindex to my form id "register1".我有以下代码将 tabindex 分配给我的表单 ID“register1”。 I would like to place the cursor on the first input or select list item on the form (item with tabindex = 1) once tabindexes are assigned.一旦分配了 tabindexes,我想将 cursor 放在第一个输入或 select 表单上的列表项(tabindex = 1 的项)上。 but the following line: $('#register1').find('input').attr('tabindex',1).select();但以下行: $('#register1').find('input').attr('tabindex',1).select(); Resets tabindex of all the inputs.重置所有输入的 tabindex。

Full code:完整代码:

$(function(){
    var tabindex = 1;
    $('#register1').find('input,select').each(function() {
        if (this.type != "hidden") {
            var $input = $(this);
            $input.attr("tabindex", tabindex);
            tabindex++;
        }
    });
    $('#register1').find('input').attr('tabindex',1).select();
});

thanks谢谢

Try:尝试:

$('#register1').find('input[tabindex=1]').whatyouwant()

Simply select the item with tabindex one in your loop using a condition:只需 select 使用条件在循环中使用 tabindex 1 的项目:

$(function(){
    var tabindex = 1;
    $('#register1').find('input,select').each(function() {
        if (this.type != "hidden") {
            var $input = $(this);
            $input.attr("tabindex", tabindex);

            // select the first one.
            if (tabindex == 1) {
               $input.select();
            }
            tabindex++;
        }
    });
});

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

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