2017-02-18 7 views
1

次のスクリプトは動作しません...エラーは何ですか?私は入力が最大の長さの限界に達すると、次のフォームフィールドにフォーカスをジャンプしようとしています。入力が最大長限界に達したときに次のフォームフィールドにフォーカスを移動する方法は?

<!DOCTYPE html> 
<html > 
<head> 
    <meta charset="UTF-8"> 
    <title>SECURITY</title> 
     <link rel="stylesheet" href="css/style.css"> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script> 
    <script type="text/javascript"> 
    $(function(){ 
     $('#focus1,#focus2,#focus3').keyup(function(e){ 
      if($(this).val().length==$(this).attr('maxlength')) 
       $(this).next(':input').focus() 
     }) 
    }) 
    </script> 
</head> 

<body> 
<div class="container"> 
    <form id="contact" action="" method="post"> 
    <h3SECURITY</h3> 
    <fieldset> 
     <input id="focus1" placeholder="Barcode" type="text" tabindex="1" maxlength="1" size="1" required> 
    </fieldset> 
    <fieldset> 
     <input id="focus2" placeholder="Identification Number" type="text" tabindex="2" maxlength="1" size="1" required> 
    </fieldset> 
     <input id="focus3" placeholder="Truck Number" type="text" tabindex="3" maxlength="1" size="1" required> 
    </fieldset> 
    <fieldset> 
     <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button> 
    </fieldset> 
    </form> 
</div> 

</body> 
</html> 

答えて

0
だけで終わる bodyタグ '</body>'前に、あなたの scriptセクションを移動し

$(function() { 
    $('#focus1,#focus2,#focus3').keyup(function(e) { 
    if ($(this).val().length >= $(this).attr('maxlength')) { 
     $(this).parent().next('fieldset').find('input').focus(); 
    } 
    }); 
}); 

とそれのdemoを:ここで

<script type="text/javascript"> 
$(function(){ 
    $('#focus1,#focus2,#focus3').keyup(function(e){ 
     if($(this).val().length==$(this).attr('maxlength')) 
      $(this).next(':input').focus() 
    }) 
}) 
</script> 

</body> 
+0

これは機能しません。 –

+0

jquery .next()関数は、最初の兄弟要素のみを対象としています。あなたの場合、次の入力は兄弟ではなく、 "いとこ"です。つまり、出かける(親をターゲットにする)ことを意味し、次に親の兄弟、そしてその子を入力する必要があります。 –

1

は働くバージョンです。

関連する問題