用于选择 ID:1 的 jQuery 选择器
我有以下带有不寻常 ID/Name 属性的选择:
<select name="customfield_10021:1" id="customfield_10021:1" class="select cascadingselect-child">
这似乎不允许我选择它:
unit = $('#customfield_10021:1 option:selected').text();
uncaught exception: Syntax error, unrecognized expression: 1
我怎样才能选择这个字段?我以前从未见过这种特殊的语法,但它显然适用于提交。
谢谢!
贾里德
I have the following select with an unusual ID/Name attribute:
<select name="customfield_10021:1" id="customfield_10021:1" class="select cascadingselect-child">
This doesn't appear to allow me to select it with:
unit = $('#customfield_10021:1 option:selected').text();
uncaught exception: Syntax error, unrecognized expression: 1
How can I select this field? I've never even seen this particular syntax before, but it apparently works on submit.
Thanks!
Jared
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用
\\
转义:
更新了 jsfiddle
或者您可以使用
document.getElementById("customfield_10021:1")
作为选择器的上下文。Escape the
:
by using\\
Updated jsfiddle
Or you can use
document.getElementById("customfield_10021:1")
as the context for your selector.你应该转义冒号,所以这样做:
你会:
那是因为冒号是一个特殊的字符,需要用反斜杠转义。
希望这有帮助。干杯
You should escape the colon, so do this:
You'd have:
That's because the colon is a special caracter, and needs to be escaped with backslash.
Hope this helps. Cheers
转义冒号:
请参阅 jQuery 常见问题解答。
您可能只想使用
.val()< 而不是使用
option:selected
/code>获取
Escape the colon:
See the jQuery FAQ.
Instead of using
option:selected
you might want to just use.val()
to get the value of the<select>
:试试这个:
:
通常用于伪选择器,并且反斜杠需要在字符串文字中加倍。为避免疑义,冒号字符是合法的 在 ID 和 Name 属性中。
Try this:
The
:
is normally used for pseudo selectors, and the backslash needs to be doubled up in the string literal.For the avoidance of doubt, the colon character is legal in ID and Name attributes.
这有效:
现场演示: http://jsfiddle.net/stzPQ/2/< /a>
This works:
Live demo: http://jsfiddle.net/stzPQ/2/
我认为这有效:
I think that works:
请记住,冒号用于分隔伪类,就像使用
option:selected
一样,您必须从元素的 id 中删除冒号:这是因为 jQuery 正在尝试使用 < code>#customfield_10021:1 as id =
customfield_10021
,其伪类1
不存在,因此出现错误。Remember that a colon is used to deliminate a pseudo class, just as you do with
option:selected
you will have to remove the colon from the id of your element:This is because jQuery is trying to use
#customfield_10021:1
as id =customfield_10021
with a pseudo class of1
which doesn't exist, thus the error.JSF 中的另一个解决方案是防止表单名称添加到每个 id 前面。
这可以通过表单标签中的 prependId="false" 来完成:
Another solution in JSF is to prevent the form name from prepending each id.
This can be done with the prependId="false" in the form tag :