2017-12-11 14 views
3

ユーザーがその中をクリックすると、ボタンにフォーカスを置く必要があるテキストボックスがあります。入力キーを使用する必要はありません。ユーザーがテキストボックス内をクリックしたときのボタン。テキストボックスをクリックしたときにボタンをフォーカスする方法

$("#txtBox").click(function() { 
 

 
    alert("The textbox is clicked."); //added for testing purposes it doesnt get hit. 
 
    $("#button").focus(); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-group col-sm-2"> 
 
    <label for="txtBox" class="sr-only">Text</label> 
 
    <input type="text" class="form-control" id="txtBox" name="txtBox"></div> 
 
<div class="form-group col-sm-1"> 
 
    <label for="button" class="sr-only">submit</label> 
 
    <button type="button" class="btn btn-default" id="button" name="button">Focus Me</button></div>

問題は、私のHTMLは、そのだから

+0

ますので、(AJAまたは他のいくつかの並べ替えによって)動的にHTMLを取得していますか? – bipen

+1

私はあなたのコードを試しました、それは期待どおりに実行されています。テキストボックスをクリックすると警告が表示され、[OK]をクリックしてフォーカスをボタンに変更しました。 –

+0

あなたのコードは正常に動作します。更新された参照してください。 – Pedram

答えて

1

txtBoxを検出していませんscript.so SQLに格納されてコメントによると、あなたはajax経由htmlをロードすることです、あなたDOMの後にhtmlを追加してください。ライブ(バインド)の機能を使用する必要がありますので、.onのように使用できますS:

$("#txtBox").on('click',function() { 
 

 
    alert("The textbox is clicked."); //added for testing purposes it doesnt get hit. 
 
    $("#button").focus(); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-group col-sm-2"> 
 
    <label for="txtBox" class="sr-only">Text</label> 
 
    <input type="text" class="form-control" id="txtBox" name="txtBox"></div> 
 
<div class="form-group col-sm-1"> 
 
    <label for="button" class="sr-only">submit</label> 
 
    <button type="button" class="btn btn-default" id="button" name="button">Focus Me</button></div>

それとも

$(document).on('click','#txtBox',function() { 
 

 
    alert("The textbox is clicked."); //added for testing purposes it doesnt get hit. 
 
    $("#button").focus(); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-group col-sm-2"> 
 
    <label for="txtBox" class="sr-only">Text</label> 
 
    <input type="text" class="form-control" id="txtBox" name="txtBox"></div> 
 
<div class="form-group col-sm-1"> 
 
    <label for="button" class="sr-only">submit</label> 
 
    <button type="button" class="btn btn-default" id="button" name="button">Focus Me</button></div>

+1

yesssssss!ありがとう、第二の選択肢が働いた – Sarah

+0

@サラあなたの歓迎 – Pedram

関連する問題