考虑一个输入元素
<input id="meta[152][value]" type="text" />
此处的输入字段是动态生成的。我需要选择那个字段。所以我用了,
alert($('#meta[152][value]').val());
但这似乎是无效的。搜索后我发现,“方括号”需要像 #meta\\[152\\]\\[value\\]
那么怎么做呢? 我目前使用此代码,
var id = "#meta[152][value]" // (I get this value by another method) I need the escaping to be done here. So that i can use as
/** 我需要使用正则表达式、替换或任何其他方法转义 id 的值 得到#meta\[152\]\[value\] 而不是手动**/
alert($(id).val());
您的建议会很有帮助!
最佳答案
以下应该有效:
alert($('#meta\[152\]\[value\]').val());
或
var id = "#meta\[152\]\[value\]";
alert($(id).val());
转换函数:
function ConvertValue(id)
{
var test = id.replace(/[[]/g,'\\\\[');
return "#" + test.replace(/]/g,'\\\\]');
}
关于javascript - jquery 转义方括号以选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8404037/