Tam*_*ege 312
如果已存在,则计算结果为true:
$("#yourSelect option[value='yourValue']").length > 0;
Run Code Online (Sandbox Code Playgroud)
小智 20
使用jQuery的另一种方法:
var exists = false;
$('#yourSelect option').each(function(){
if (this.value == yourValue) {
exists = true;
}
});
Run Code Online (Sandbox Code Playgroud)
Seb*_*Seb 19
if ( $("#your_select_id option[value=<enter_value_here>]").length == 0 ){
alert("option doesn't exist!");
}
Run Code Online (Sandbox Code Playgroud)
aln*_*h29 16
var exists = $("#yourSelect option")
.filter(function (i, o) { return o.value === yourValue; })
.length > 0;
Run Code Online (Sandbox Code Playgroud)
这具有为您自动转义值的优点,这使得文本中的随机引号更容易处理.
Str*_*bek 12
虽然大多数其他答案对我有用,但我使用了 .find():
if ($("#yourSelect").find('option[value="value"]').length === 0){
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
138380 次 |
最近记录: |