0

特定の値を持つオプションが選択フィールドに存在するかどうかを確認するにはどうすればよいですか?

<select>
  <option value="o1">Option 1</option>
  <option value="o2">Option 2</option>
</select>

select の値が "o1" = true の場合

select の値が "o4" = false の場合

4

2 に答える 2

1

id選択にフィールドを追加する場合は、次のようにしid="selector"ます。

var x = document.getElementById("selector").options;
for(var i=0; i<x.length; i++){
    if (x[i].value == "o1"){
    ...

于 2013-07-31T22:15:26.040 に答える
1

オプションをループして、その値を確認できます。

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'
  }
}
于 2013-07-31T22:17:54.390 に答える