[英]JQuery doesn't recognize dynamically generated id
I have searched for a while, but I'm not able to find the solution to this problem. 我搜索了一段时间,但我无法找到解决这个问题的方法。
I am working with JSPs and Servlets. 我正在使用JSP和Servlet。 I run a JSP where some code is generated dynamically.
我运行一个JSP,动态生成一些代码。
When I launch the program, I can see everything correctly. 当我启动程序时,我可以正确地看到一切。 In fact I have opened the source code of the webpage, and everything seems to be correct.
事实上我已经打开了网页的源代码,一切似乎都是正确的。
In that code, it appears a <td>
tag with the id PAR1.1:ED 在该代码中,它显示一个id为PAR1.1:ED的
<td>
标记
<.td id="PAR1.1:ED" >3700.0</td>
(I have added the . here on purpose. There is no point in the code :P) (我在故意添加了。这里没有代码:P)
Then I have a script where I try to search that id, but it seems as if it doesn't exist. 然后我有一个脚本,我尝试搜索该ID,但似乎它不存在。
<script type="text/javascript">
$(document).ready(function() {
$(document).on("change", "input", function() {
if( $("#PAR1.1:ED").length ){
alert("exists");
}
else{
alert("doesn't exist");
}
});
});
</script>
How can I make that tag id be detected? 如何检测标签ID?
Thank you very much! 非常感谢你!
The problem is with the colon and the periods; 问题在于结肠和时期; you need to escape them in your selector :
你需要在你的选择器中转义它们 :
if ($("#PAR1\\.1\\:ED").length ){
alert("exists");
}
It's a common problem in JSF, too, where lots of generated ids have a colon inside. 这也是JSF中的常见问题,其中许多生成的id内部都有冒号。 While it's not forbidden to use a colon as an id, it conflicts with the meaning of a pseudo-class in a CSS selector.
虽然不禁止使用冒号作为id,但它与CSS选择器中伪类的含义相冲突。
Try this : 尝试这个 :
The issue is that jquery selector would conflict if you are looking for an id using '#' having characters ( such as period) . 问题是,如果您正在使用带有字符(例如句点)的“#”查找id,则jquery选择器会发生冲突。 If you were to use partial selectors, it would be okay.
如果你使用部分选择器,那就没关系。
if( $("[id^='PAR1.1:ED']").length)
{
alert("exists");
}
else{
alert("doesn't exist");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.