.focus()不能工作,因为动态表还没有加载
.focus() does not work because dynamic table not loaded yet - JQUERY
我正在使用来自AJAX调用的数据动态创建一个表。我使用一个"选择/下拉框"。on('change')函数发送值到我的PHP。一旦表显示出来,我想将焦点设置为在表上动态创建的搜索框。
问题似乎是,由于表需要几秒钟才能显示,因此.focus()命令在表可见之前触发。我想有。focus()命令火后创建表函数完成。
Javascript/Jquery代码:$('#t6F3Box1').on('change', function()
{
var $newTable ='<table id="tempEmployerTbl" class="studentTables"><thead><tr>';
var $searchStr=new RegExp("ID");
var $tableContainerDiv="#t6F3TableCont";
var $rowFlag=0;
var $responseDataValue=[]
var $employerState=$('#t6F3Box1').val();
var $getTableDataPhp="../application/employer.php";
var $data=[];
$('#t6F3ErrDiv').text('');
$('#t6F3TableCont, #t6F3HLine2, #t6F3HLine2').show();
$data += "&employerState=" + encodeURIComponent($employerState);
$.ajax({
type: 'POST',
async: false,
url: $getTableDataPhp,
cache: false,
datatype: 'json',
data: $data,
success: function($responseData)
{
//Loop through the responsdata returned in a JSON Array
//dynamically create the Employer drop down box
if (!$responseData.authenticationError)
{
//Create the header for the Employer Table
$.each($responseData, function($key, $value)
{
if($rowFlag==0)
{
$responseDataValue=$responseData[$key];
$.each($responseDataValue, function($keys, $values)
{
if ($searchStr.test($keys))
{
$newTable += '<th class="tableDataHide">' + $keys + '</th>';
}
else
{
$newTable += '<th class="tableDataShow">' + $keys + '</th>'
}
});
$rowFlag=1;
}
});
$newTable +='</tr></thead><tbody>';
//Create the body for the Employer Table
$.each($responseData, function($key, $value)
{
$newTable += '<tr>'
$responseDataValue=$responseData[$key];
$.each($responseDataValue, function($keys, $values)
{
if ($searchStr.test($keys))
{
$newTable += '<td class="tableDataHide">' + $values + '</td>';
}
else
{
if($values==null)
{
$values="";
}
$newTable += '<td class="tableDataShow">' + $values + '</td>'
}
});
$newTable +='</tr>';
});
$newTable +='</tbody></table>';
$($tableContainerDiv).html($newTable);
$("#tempEmployerTbl").dataTable();
$('#tblSearchBox').focus();
}
}
});
我不知道你甚至在你的例子中调用.focus
的地方,但解决方案是简单地在$($tableContainerDiv).html($newTable);
之后调用它——即在它被ajax回调追加之后。
相关文章:
- 关于使用.className的建议,目前还没有;我不能在我的圈子里工作
- 为什么动态表没有显示边框
- 动态删除没有唯一属性的输入字段
- 我的锚属性的动态onclick没有启动
- 如何调用$scope$apply()仅当它's还没有进行中
- 承诺取消方法.为什么它还没有实现,而不是其他承诺框架
- Knockoutjs:如果还没有值,则级联写入多个可观察量
- {还没有答案}从 YouTube JSON API 获取评论分数
- 如何在 jQuery 中重新触发音频,当它还没有完成
- Facebook Javascript SDK - Friends List with Picture 谁还没有安装我的
- 当D3.js上还没有现有节点时,调用selectAll有什么意义
- $. Mustache()已经定义,但是当Mustache()被调用时,Mustache还没有定义
- Javascript/jQuery:控制执行顺序(还没有ajax/php)
- Backbone.js DOM还没有准备好在渲染方法中应用jquery的魔力
- Angular单元测试:浏览器还没有定义
- 铬,Firebase推送通知服务工作者还没有准备好
- 为什么我的动态表没有过滤功能?
- 在第二次调用geteneration时,集合还没有初始化
- 错误:集合还没有初始化
- Angular js、ng-model还没有为第三方指令做好准备