<select id='test' size='1'><option value='this"'>this"</option></select>
这些都不起作用:
if ($('#test option[value="this""]').length == 0 ) {
$('body').append('true');
}
else {$('body').append('false');}
if ($("#test option[value='this"']").length == 0 ) {
$('body').append('true');
}
else {$('body').append('false');}
编辑:
这个问题可能过于简单化了。值字符串实际上是一个从另一个源派生的变量,所以我不知道在哪里添加转义字符。
我本以为: var temp = mySTRING.replace('\"','\\\"');
会起作用,但它没有......
最佳答案
您可以将过滤函数与正则表达式结合使用来获取值包含 '
或 "
的所有选项
var x = $('#test option').filter(function () {
return /'|"/.test(this.value);
});
if (x.length) {
$('body').append(x.length + ' true');
} else {
$('body').append('false');
}
关于javascript - 如何搜索其值可能在字符串中包含“或”的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16577255/