【问题标题】:jQuery - change focus on next inputjQuery - 改变对下一个输入的关注
【发布时间】:2013-11-16 03:10:12
【问题描述】:

我有这个代码

<form method="post" class="form-horizontal" role="form">
  <label for="qt" class="control-label">Qt</label>
  <input type="text" class="form-control" id="qt" name="qt" min="1" step="1" />
  <label for="code" class="control-label">Code</label>
  <input type="text" class="form-control" id="code" name="code" placeholder="code" />
  <button type="submit" class="btn btn-default">Submit</button>

操作员使用条形码扫描仪将数据插入输入字段,因此我必须创建一个 jQuery 代码,当“qt”已更改时自动关注下一个输入,并在“代码”已更改时提交表单改变了。

我读了another similar post,然后写了这个:

$(document).ready(function(){
  $("#qt").change(function () {
    $(this).nextAll("input").first().focus();
  });
  $("#code").change(function () {
    this.form.submit();
  });
});

但它不能正常工作。你可以试试jsfiddle.net

【问题讨论】:

    标签: jquery submit autofocus


    【解决方案1】:

    来自您的帖子:

    我有这个代码

    嗯,事实上,你没有。您的 jsFiddle 链接中的代码非常不同。不是您在问题中显示的平面结构,而是嵌套结构。 nextAll 仅在当前元素的兄弟姐妹中搜索(不是在整个 HTML 中)。

    您的遍历方法需要更复杂。这样的事情会做:

    $(this).closest('div.control-group').next().find('input').first().focus();
    

    fiddle

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 2020-05-14
      • 2013-03-13
      • 1970-01-01
      • 2015-08-13
      相关资源
      最近更新 更多