jQuery选择器无法与[]一起使用 [英] jquery selector not working with [ ]
问题描述
我正在使用jQuery在osCommerce实例中处理表单数据,但在选择某些元素时遇到了麻烦.
I'm using jQuery to manipulate form data in an osCommerce instance and I'm having trouble selecting some elements.
该脚本生成ID为product_description [N]的文本区域,N为1,2,3 ...
The script generates textareas with the id product_description[N], N being the 1, 2, 3...
问题在于,其中带有[]的id值会使jQuery(甚至是常规脚本)无法选择该元素,并且我无法使用它.
The problem is that having an id value with [] in it makes jQuery (even regular script) not to select the element and I can't work with it.
我尝试将id切换为下划线,处理信息,然后将其更改回[],但我什至不能这样做:
I tried switching the id to underscores, manipulate the information and then change it back to [ ] but I can't even do:
$('#product_description[1]').attr('id','products_description_1');
有没有一种方法使jQuery选择类似$('#some [2]').function ...的东西??
Is there a way to make jQuery to select something like this $('#some[2]').function... ?
如果没有,如果还有另一种方法可以更改ID值,则可以,因为我可以像往常一样工作,然后将其更改回[],以便php识别该字段
If not, If there's another way I can change the ID value then it's ok because I can work as usually and then change it back to the [ ] for the php to recognize that field
是的,我知道,我可以按照类的其他方式选择文本区域,但是由于te页面中有很多texareas,因此我需要一个唯一的名称,这将需要编辑我不需要的osCommerce脚本.我打算将我的JS贡献给oscommerce社区,并且让另一个人添加.js很容易,但是如果他们必须编辑php文件以使javascript正常工作,对于新手来说可能太吓人,或者对于某人来说可能是不可能的已经对其进行了编辑.
Yes, I know, I can select the textarea in another way like by class, but as there are many texareas in te page, I need a unique name and that would requiere editing osCommerce script which I don't want. I plan to contribute my JS to the oscommerce community and for another person to just add a .js is easy, but if they have to edit a php file for the javascript to work it could be either too scary for a newbie or impossible for somebody that already edited it.
非常感谢
推荐答案
尝试一下:
$('#product_description\\[1\\]')
请注意,尽管这可能可行,但括号字符不适用于HTML5之前的ID(尽管可以在类中使用).
Note that while this may work, the bracket characters are not valid for use in IDs prior to HTML5 (although they're fine for use in classes).
这篇关于jQuery选择器无法与[]一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!