如何检查选择字段中是否存在具有特定值的选项?
<select>
<option value="o1">Option 1</option>
<option value="o2">Option 2</option>
</select>
如果 select 的值为“o1”= true
如果 select 的值为“o4”= false
最佳答案
您可以循环选项并检查它们的值:
var select = document.getElementById('selectID') ||
document.forms[formName or index].elements[selectName or index];
var options = select.options
for (var i=0, iLen=options.length; i<iLen; i++) {
if (options[i].value == 'foo') {
// found option with value 'foo'
}
}
关于javascript - 如何在没有 jQuery 的情况下检查选择字段是否具有特定选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17981894/