我div在同一个表格中有几个.我想要做的是禁用表单Tab中的一个中的键,div而不div以相同的形式禁用其他s中的选项卡.
示例表格:
小智 181
一个简单的方法是将tabindex =" - 1"放在您不希望被选中的字段中.例如
<input type="text" tabindex="-1" name="f1">
Run Code Online (Sandbox Code Playgroud)
the*_*ter 12
与Yipio类似,我notab="notab"作为属性添加到我想要禁用选项卡的任何元素.我的jQuery就是一行.
$('input[notab=notab]').on('keydown', function(e){ if (e.keyCode == 9) e.preventDefault() });
Run Code Online (Sandbox Code Playgroud)
顺便说一句,keypress对许多控制键不起作用.
基于Terry的简单答案,我将其变成了一个基本的jQuery函数
$.prototype.disableTab = function() {
this.each(function() {
$(this).attr('tabindex', '-1');
});
};
$('.unfocusable-element, .another-unfocusable-element').disableTab();
Run Code Online (Sandbox Code Playgroud)
小智 1
您必须禁用或启用各个元素。我就是这样做的:
$(':input').keydown(function(e){
var allowTab = true;
var id = $(this).attr('name');
// insert your form fields here -- (:'') is required after
var inputArr = {username:'', email:'', password:'', address:''}
// allow or disable the fields in inputArr by changing true / false
if(id in inputArr) allowTab = false;
if(e.keyCode==9 && allowTab==false) e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)