我有一个文本框,当用户点击它时,它应该把重点放在按钮上。我不需要使用回车键按。我只想在用户点击文本框内时专注于按钮
$("#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存储在SQL脚本中。因此它没有检测到txtBox
答案 0 :(得分:1)
因此,根据评论,您通过html
加载ajax
,在html
之后追加DOM
,因此您需要使用直播< em>(bind)函数,您可以像这样使用.on
:
$("#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>