如何逃避这个选择器使它工作?

cha*_*nyi 0 html javascript jquery escaping jquery-selectors

我有收音机

<input type="radio" name="<_WallForm>" value="=(1,2,3)">
Run Code Online (Sandbox Code Playgroud)

用脚本

n = n.replace('<', '\\<').replace('>', '\\>').replace('=', '\\=');
v = v.replace('<', '\\<').replace('>', '\\>').replace('=', '\\=');

f.find('input[type=radio][name=' + n + '][value=' + v +']').parent().parent().find('.selectPic').click();
Run Code Online (Sandbox Code Playgroud)

错误

Uncaught Error: Syntax error, unrecognized expression: input[type=radio][name=\<_WallForm\>][value=\=(1,2,3)]

怎么办?谢谢.

Bol*_*ock 6

你不需要逃避任何事情.只需在属性选择器中添加引号(并删除前几行):

f.find('input[type="radio"][name="' + n + '"][value="' + v +'"]').parent().parent().find('.selectPic').click();
Run Code Online (Sandbox Code Playgroud)

这应该会产生一个选择器

input[type="radio"][name="<_WallForm>"][value="=(1,2,3)"]
Run Code Online (Sandbox Code Playgroud)

...您将注意到的内容类似于HTML中引用属性值的方式.


在一个侧面说明,你需要编码的尖括号,以便您的HTML验证:

<input type="radio" name="&lt;_WallForm&gt;" value="=(1,2,3)">
Run Code Online (Sandbox Code Playgroud)

但是您不必在选择器中反映这一点 - 事实上,如果您尝试这样做,则选择器可能会停止匹配,因为选择器中无法识别HTML实体引用; 他们只是字面意思.

正如评论中所提到的,如果您避免使用尖括号等特殊字符以及&尽可能使用任意值的符号,您的生活会更容易.