0

I have this code

<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>

An operator uses a barcode scanner to insert the data into input fields, so I have to create a jQuery code that automatically focuses on the next input when "qt" has been changed and submit the form when the "code" has been changed.

I read another similar post and I wrote this:

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

but it does not work properly. You can try on jsfiddle.net

4

1 回答 1

1

从你的帖子:

我有这个代码

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

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

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

小提琴

于 2013-11-05T09:41:59.203 回答