开发者

How to set focus to a textbox in a jquery dialog window?

Is there a way to set focus to an input textbox inside jquery dialog window? I have looked at some other question over here but could not find the answser.

I have tried ..

, open: function() { $(this).find("#inp开发者_开发百科").focus(); }

but it didn't work.

Any ideas? Thanks.

UPDATE

the content of dialog window is fetched thru an ajax call.


Try

$("#inp").focus();

But if you are loading the dialog content with AJAX then you should put focus in call back function

$('#result').load('ajax/test.html', function() {
  $("#inp").focus();
});


So, your open function works, right? Have you checked if it finds #inp? Just do

console.log($('#inp'));

And see if the element is actually there at the time focus() is being called. If it is not, then maybe the DOM wasn't finished being constructed (I'm assuming, your open() function is called from within said Ajax call's callback, right?).

The 'success' and 'error' Ajax-callbacks are being called after the call has finished, but not necessarily after all resulting DOM-manipulations are finished. One (slightly hacky) way I solved this in the was by delaying the focus for a few milliseconds:

setTimeout(function () {
    $(this).find("#inp").focus();
}, 50);

This gives the DOM a bit of time to put #inp in place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜