2
<select id='test' size='1'><option value='this&#34;'>this&#34;</option></select>

这些都不起作用:

if ($('#test option[value="this&#34;"]').length == 0 ) {
   $('body').append('true');
}
else {$('body').append('false');}
if ($("#test option[value='this&#34;']").length == 0 ) {
   $('body').append('true');
}
else {$('body').append('false');}

小提琴

编辑:

这个问题可能过于简单了。值字符串实际上是一个从另一个来源派生的变量,所以我不知道在哪里添加转义字符。

我本来会这样想:var temp = mySTRING.replace('\"','\\\"');会起作用,但它没有......

4

2 回答 2

2

您可以使用带有正则表达式的过滤器函数来获取所有选项,其值包含'"

var x = $('#test option').filter(function () {
    return /'|"/.test(this.value);
});
if (x.length) {
    $('body').append(x.length + ' true');  
} else {
    $('body').append('false');
}

小提琴

于 2013-05-16T01:04:14.533 回答
1

Use the escape character, to match the quotation mark.

if ($("#test option[value='this\"']").length == 0 ) {
    $('body').append('true');
}
else {$('body').append('false');}

UPDATE

In response to your update, you said that the value string is inside a variable, thus you can replace it with the escape character.

var value = "this&#34;";
value = value.replace("&#34;", "\"");

if ($("#test option[value='" + value  + "']").length == 0 ) {
    $('body').append('true');
}
else {$('body').append('false');}

Check the working JsFiddle here

于 2013-05-16T00:46:52.283 回答