简体   繁体   English

通过javascript自动对焦新添加的元素

[英]Autofocusing on a newly added element via javascript

This should be simple but I can't get it to work so would appreciate any pointers. 应该很简单,但我不能让它工作,所以会欣赏任何指针。

I have a function in my webpage that adds a new line into a table with inputs etc. This is a pretty basic POST that looks something like this... 我在我的网页中有一个功能,它将一个新行添加到带有输入等的表中。这是一个非常基本的POST,看起来像这样......

$.post(url, {
    newRow : newRow
}, function(data) {
    $('#' + tableName + ' > tbody > tr').eq(rowLoc).before(data);
}).done(function() {
    reNumberTableIDName($tb)
});

The reNumberTableIDName function is pretty straightforward, it runs through all the objects in the table and changes their id/name so that they are in order relevant to the row they are in the table for other reasons. reNumberTableIDName函数非常简单,它遍历表中的所有对象并更改其id / name,以便它们与其他表中的行相关。

Now, one of the elements added in to this table is an auto complete input which looks like so... 现在,添加到此表中的一个元素是自动完成输入,看起来像这样......

<input id="autoLook[9]" class="required yui-ac-input" type="text" title="" value="" name="autoLook[9].id" style="width:500px" autocomplete="off" required="required">

Note that the name and ID are not altered as part of the reNumberTableIDName function. 请注意,名称和ID 不会作为reNumberTableIDName函数的一部分进行更改。

I tried adding in a line to the "done"portion of the post to then put the focus/carat into that new input but it doesn't work (focus stays on the button previously clicked to add the row). 我尝试在帖子的“完成”部分添加一行,然后将焦点/克拉放入新输入但它不起作用(焦点停留在先前单击以添加行的按钮上)。

$("#autoLook[" + (newRow -1) + "]").focus();

I've checked with an alert that "#autoLook[" + (newRow -1) + "]" does indeed come up with the right string so I'm at a loss as to why this doesn't work. 我已经检查过一个警告"#autoLook[" + (newRow -1) + "]"确实提出了正确的字符串,所以我不知道为什么这不起作用。 What obvious litle gotcha am I missing? 我错过了什么明显的谎言?

Its also worth noting that I tried to add in a autofocus property to the input being added, but I'm working in grails and this type of auto complete doesn't allow me to do this. 还值得注意的是,我试图在添加的输入中添加自动聚焦属性,但我正在使用grails,这种类型的自动完成不允许我这样做。

Thanks! 谢谢!

Worth noting that the solution should be to use setTimeout as mentioned below else it will continually put focus back to the object! 值得注意的是,解决方案应该是使用如下所述的setTimeout,否则它会不断地将焦点放回到对象!

The selector #autoLook[9] matches an element with the id autoLook and the attribute 9 ; 选择器#autoLook[9]匹配一个id为autoLook且属性为9的元素; which is obviously wrong. 这显然是错的。 Add backslashes around the square brackets to escape them: 在方括号周围添加反斜杠以逃避它们:

$("#autoLook\\[" + (newRow - 1) + "\\]").focus();

Quote from jQuery Documentation: Selectors : 引用jQuery文档:选择器

To use any of the meta-characters (such as !"#$%&'()*+,./:;<=>?@[\\]^``{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\\\ . For example, an element with id="foo.bar" , can use the selector $("#foo\\\\.bar") . The W3C CSS specification contains the complete set of rules regarding valid CSS selectors. Also useful is the blog entry by Mathias Bynens on CSS character escape sequences for identifiers. 使用任何元字符(例如!"#$%&'()*+,./:;<=>?@[\\]^``{|}~ )作为名称的文字部分,它必须使用两个反斜杠转义: \\\\ 。例如,一个id="foo.bar"的元素,可以使用选择器$("#foo\\\\.bar") 。W3C CSS规范包含完整的有关有效CSS选择器的规则集。还有一个有用的是Mathias Bynens关于标识符的CSS字符转义序列的博客条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM