teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正

2017/08/24 19:27

投稿

s8_chu
s8_chu

スコア14731

answer CHANGED
@@ -19,15 +19,15 @@
19
19
  <input id="text3" type="text" placeholder="3">
20
20
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
21
21
  <script>
22
- var i = 4;
22
+ var i = 4, s = "input[id^=text]";
23
23
  $("#button1").on("click", function () {
24
24
  $("body").append("<input id=text" + i + " type='text' placeholder=" + i + ">\n");
25
25
  i += 1;
26
26
  });
27
27
 
28
- $(document).on("keyup", "[id^=text]", function (e) {
28
+ $(document).on("keyup", s, function (e) {
29
29
  if (e.keyCode === 13) {
30
- $(this).attr("id") === $("input[id^=text]").last().attr("id") ? $(this).prevAll("[id^=text]").last().focus() : $(this).nextAll("input[id^=text]").eq(0).focus();
30
+ $(this).attr("id") === $(s).eq(-1).attr("id") ? $(this).prevAll(s).eq(-1).focus() : $(this).nextAll(s).eq(0).focus();
31
31
  }
32
32
  });
33
33
  </script>