[英]Setting Focus on dynamically created input field without knowing its ID
我有一个jQuery / AJAX函数,可从服务器加载HTML表单元素。 表单上的字段会有所不同。 加载完成后,我想将焦点设置在第一个可用的输入字段上。 我试过了:
$(':input:enabled:visible:first').focus();
但这不起作用:(我不确定如何在jQuery中处理输入字段。我不知道返回的元素的ID。
我的函数如下所示,而my_html变量包含许多简单的输入字段,例如
<input type='text' name='test' value='prefilled' />
AJAX功能:
$.loadIDX = function (idx, position){
var url = 'URL HERE';
$.ajax({
url: url,
type: "POST",
data: {
idx:idx,
position:position
},
dataType: 'json',
success: function(data) {
if(data.status=='request'){
var my_html = '<div id="slide_in"><div id="please_enter_'+idx+'" class="please_enter">Please enter:</div><form id="cform_'+idx+'">';
// loop through json & create html input form fields
$.each(data, function(key, val) {
if(key!='status'){
my_html += val;
}
})
// close up the form
my_html += '<input class="submit_button" type="button" value="ok" onClick="postForm('+idx+','+position+')" /></form></div>';
// place the new HTML into the DIV
$('#yn_'+idx).html( my_html );
// set focus... HOW?
$(':input:enabled:visible:first').focus();
} else {
$.closeIDX(idx);
}
}
});
}
怎么样
$('#yn_'+idx).html( my_html ).find('input:first').focus();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.