[英]How to check if a select field has a certain option value without jQuery
如何检查选择字段中是否存在具有特定值的选项?
<select>
<option value="o1">Option 1</option>
<option value="o2">Option 2</option>
</select>
如果select的值为“o1”= true
如果select的值为“o4”= false
如果你要在select中添加一个id
字段,比如id="selector"
你可以这样做:
var x = document.getElementById("selector").options;
for(var i=0; i<x.length; i++){
if (x[i].value == "o1"){
...
等等
您可以遍历选项并检查其值:
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'
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.