开发者

Can't set focus() on a text field

开发者 https://www.devze.com 2023-01-28 15:12 出处:网络
This is just the weirdest thing. I\'ve got a Sammy.js app, and I want to set focus on a text field right after the HTML loads. I\'ve got this CoffeeScript here:

This is just the weirdest thing. I've got a Sammy.js app, and I want to set focus on a text field right after the HTML loads. I've got this CoffeeScript here:

this.partial('templates/my-template.jqt').then ->
  i = $('#item')
  debugger
  i.focus()

While I'm in the debugger, right on that line, I can inspect "i" and see that it's a JQuery object. I can even call i.val("HI THERE!") and see my text field update. But, calling i.focus() does absolutely nothing. Is there some security feature I'm missing that doesn't let you focus on a text element that was dynamically loaded开发者_运维百科?


Try:

setTimeout(function() { $('#item').focus() }, 1);

It's quite common issue. For some reason delaying the focus for 1ms makes it working.


JQuery's focus() doesn't focus on an element, it binds an event handler to focus. http://api.jquery.com/focus/

Try i.get().focus() instead.


If you are sure that you are getting $('#item') object.You can try out few things.

Make sure that you have only one input box with <input type="text" id="item" />.Same id should not be repeated.

Next thing is why dont you try $('#item').focus().

Thanks. with regards,

Wasim


Do it like this:

i.trigger 'focus'
0

精彩评论

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