使用jQuery在未标记为只读的字段上动态添加tabindex

时间:2018-07-17 19:47:23

标签: jquery html

我的表格很大,有许多动态生成的文本框。我正在尝试自动设置tabindex,但仅适用于未设置readonly属性的文本框。在这里蚕食了一些代码片段,但无法正常工作。

&& this.not('[readonly]')

我知道我在这里搞砸了

{{1}}

我确定这很简单,以为我看了太久了。

2 个答案:

答案 0 :(得分:1)

使用仅捕获所需项目的选择器,使用传递给each回调的索引来设置选项卡索引。

$(function () {
    let inputs = $('input:not([type=hidden]):not([readonly])')
    inputs.each(function(i) {
        $(this).attr('tabindex', i+1)
    })
})

答案 1 :(得分:0)

// like this
console.log( $('input').not('[readonly]').length );

// or this
console.log( $('input:not([readonly])').length );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text"/>
<input type="text" readonly="readonly"/>
<input type="text"/>

相关问题