【问题标题】:jquery if tab key pressed in text input, skip following input and go to nextjquery如果在文本输入中按下tab键,跳过以下输入并转到下一个
【发布时间】:2018-01-31 10:40:05
【问题描述】:

我有一些文本输入。如果我从#input1 中选择标签,我想跳过#input2 并转到下一个。

<div class="form-group required">
    <label for="input1" class="control-label col-sm-4">Input 1</label>
    <div class="col-sm-5">
        <input type="text" name="input1" class="form-control" id="input1" tabindex="3">
    </div>
</div>
<div class="form-group">
    <label for="input2" class="control-label col-sm-4">Input 2</label>
    <div class="col-sm-5">
        <input type="text" name="input2" id="input2" class="form-control" tabindex="4">
    </div>
</div>

<!-- This field isn't always visible and is dependent on a select list option -->
<div class="form-group required" style="display:none">
    <label for="input3" class="control-label col-sm-4">Input 3</label>
    <div class="col-sm-5">
        <input type="text" name="input3" class="form-control" id="input3" tabindex="5">
    </div>
</div>
<!-- -->

<div class="form-group">
    <label for="withdrawal_amount" class="control-label col-sm-4">Label</label>
    <div class="col-sm-5">
        <input type="text" name="label" class="form-control" id="label" tabindex="5">
    </div>
</div>

我将焦点定位到#label,但如果#input3 可见,我需要将其定位。

$('#withdrawal_amount').focusout(function() {
    $(window).keyup(function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 9) {
            $(this).next(':input').not('#input2').focus();
            // $('#label').focus();
        }
    });
});

我想在#input3 中看到可见并将焦点设置为。最好的方法是什么?

这不是重复的。

我想让焦点集中到输入#2,但我不希望 tab 键集中到字段

【问题讨论】:

  • @SurjitSD,它不是重复的。我想 ALLOW 关注该输入,而不仅仅是使用 tab 键
  • 您的问题是“我想跳过 #input2 并转到下一个。” ?你不是想跳过输入吗?如果我弄错了,请清除我
  • 我只想在#input1 中按下 Tab 键时跳过该输入。我需要随时集中注意力。

标签: jquery input


【解决方案1】:

使用 keydown 代替 keyup https://jsfiddle.net/09tchwx9/4/

 $(':text').keydown(function(e){
    var key = e.keyCode || e.which,
    ind = $(':text').index(this),
    skip=$(':text').eq(ind+1)
    if(key==9 && skip.hasClass('skip')){
       e.preventDefault();
       $(':text:not(.skip)').filter(function(i){return i> ind}).eq(0).focus();
  }
});

【讨论】:

    猜你喜欢
    • 2020-05-14
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2017-06-29
    • 2014-01-18
    • 2021-11-16
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多