2017-10-27 5 views
0

ラジオボタンを変更した後、入力フィールドのフォーカスを設定しようとしました。 警告がポップアップ表示されているため、変更アクションが発生しますが、フォーカスは入力フィールドに設定されていません。私のコードで何が間違っていますか?ラジオを変更した後の入力フィールドにどのように焦点を当てますか?

ありがとうございました!

$("input[name=options]").change(function() { 
 
    alert(this.value); 
 
    document.getElementById("input1").focus(); 
 
});
<!doctype html> 
 
<html lang="en"> 
 
    <head> 
 
    <title>Hello, world!</title> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
 

 
    <!-- Bootstrap CSS --> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous"> 
 
    </head> 
 

 
    <body> 
 
    <div class="container"> 
 
     <div class="row justify-content-center"> 
 
     <div class="btn-group" data-toggle="buttons"> 
 
      <label class="btn btn-primary active"> 
 
      <input type="radio" name="options" value="x1" id="option1" autocomplete="off" checked> Radio 1 
 
      </label> 
 
      <label class="btn btn-primary"> 
 
      <input type="radio" name="options" value="x2" id="option2" autocomplete="off"> Radio 2 
 
      </label> 
 
      <label class="btn btn-primary"> 
 
      <input type="radio" name="options" value="x3" id="option3" autocomplete="off"> Radio 3 
 
      </label> 
 
     </div> 
 
     </div> 
 
     <br> 
 
     <div class="row justify-content-center"> 
 
     <form> 
 
      <div class="input-group input-group-lg"> 
 
      <input type="number" class="form-control" id="input1" autocomplete="off"> 
 
      <span class="input-group-btn"> 
 
       <button type="submit" class="btn btn-primary btn-custom"> 
 
       Go! 
 
       </button> 
 
      </span> 
 
      </div> \t 
 
     </form> 
 
     </div> <!-- row --> 
 
    </div> 
 

 
    <!-- jQuery first, then Popper.js, then Bootstrap JS --> 
 
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script> 
 

 
    </body> 
 
</html>

答えて

0

次のコードを試してみてください。イベントをchangeからfocusに変更しました。要素の変更イベントがトリガされた後は、それに集中します。だから私はあなたがクリックした要素に戻って焦点を変更すると思います。

$("input[name=options]").focus(function() { 
 
    alert(this.value); 
 
    document.getElementById("input1").focus(); 
 
}); 
 
document.getElementById("input1").focus();
<!doctype html> 
 
<html lang="en"> 
 
    <head> 
 
    <title>Hello, world!</title> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
 

 
    <!-- Bootstrap CSS --> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous"> 
 
    </head> 
 

 
    <body> 
 
    <div class="container"> 
 
     <div class="row justify-content-center"> 
 
     <div class="btn-group" data-toggle="buttons"> 
 
      <label class="btn btn-primary active"> 
 
      <input type="radio" name="options" value="x1" id="option1" autocomplete="off" checked> Radio 1 
 
      </label> 
 
      <label class="btn btn-primary"> 
 
      <input type="radio" name="options" value="x2" id="option2" autocomplete="off"> Radio 2 
 
      </label> 
 
      <label class="btn btn-primary"> 
 
      <input type="radio" name="options" value="x3" id="option3" autocomplete="off"> Radio 3 
 
      </label> 
 
     </div> 
 
     </div> 
 
     <br> 
 
     <div class="row justify-content-center"> 
 
     <form> 
 
      <div class="input-group input-group-lg"> 
 
      <input type="number" class="form-control" id="input1" autocomplete="off"> 
 
      <span class="input-group-btn"> 
 
       <button type="submit" class="btn btn-primary btn-custom"> 
 
       Go! 
 
       </button> 
 
      </span> 
 
      </div> \t 
 
     </form> 
 
     </div> <!-- row --> 
 
    </div> 
 

 
    <!-- jQuery first, then Popper.js, then Bootstrap JS --> 
 
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script> 
 

 
    </body> 
 
</html>

0

次使用。 0〜50または100から

$("input[name=options]").change(function() { 
    window.setTimeout(function() { 
     document.getElementById("input1").focus(); 
    }, 0); 
}); 

増加のタイムアウト値任意のブラウザが警告を使用していない。また

をサポートしていなかった場合。 アラートは、あなたをテキストボックスからフォーカスします。

関連する問題