选择具有异国ID的元素
我正在使用一个在id参数中使用方括号的遗留系统。例如:
I'm working on a legacy system that uses square brackets in id parameters. E.g.:
<select id="mySelect[]">
<option value="1">Yadda</option>
<option value="2">Yadda</option>
<option value="3">Yadda</option>
</select>
jQuery(作为javaScript)在我尝试使用这个id时,非常正确地抱怨说, p>
jQuery (being javaScript) quite rightly complains when I try to use this id in, say,
$("#mySelect[]").append(options);
鉴于我不能更改旧版代码,因此我被卡住了现有的id(在任何情况下都可能没有效果),这是一个很好的办法解决这个问题,让我选择我的元素?
Given the fact that I can't change the legacy code and therefore I'm stuck with the existing id's (which may or may not be valid in any context), what would be a good way to work around this mess and let me select my elements?
使用元字符时,您必须转出这些字符。
While using meta characters you have to escapse those.
$("#mySelect\\[\\]").append(options);
要使用任何元字符(如!# $%&'()* +,。/:;< =>?@ [] ^`{|}〜)作为名称的文字部分,必须使用两个反斜杠进行转义:\。 / p>
To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \.
如果你不想逃脱它,你可以使用选择器
If you don't want to escape it, you can use the selector
$( document.getElementById('mySelect[]')