[英]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.