更改选择选项选项

时间:2011-05-10 09:01:53

标签: html html-select

我想将所选的选项更改为其他值。 即,

 <select id="myid"> 
<option value="1" selected="selected">1 </option>
<option value="2" >2 </option>
<option value="3" >3 </option>
<option value="4" >4 </option>
</select>

我必须触发此选择中的更改并选择第3个选项我有值我该怎么做。?

我每次都通过一个函数添加一个选项,我希望在每次添加该选项后,我希望它被选中以进行进一步的活动。

3 个答案:

答案 0 :(得分:2)

按索引更改

<input type="button" value="Change Select" onclick="changeSelect(3)" />

function changeSelect(x) {
    document.frm.myid.selectedIndex = x-1;
}

按值更改

<input type="button" value="Change Select" onclick="changeSelect1(3)" />

function changeSelect1(x) {
  var myid = document.getElementById('myid');
  var st = myid.options;
  for(var i=0; i<st.length; i++) {
    if(st[i].value == x) {
      myid.selectedIndex = i;
    }
  }
}

答案 1 :(得分:1)

function Change() {
            for (i = 0; i < document.getElementById('ddStream').options.length; i++) {
                if (document.getElementById('ddStream').options[i].value == 1)
                document.getElementById('ddStream').selectedIndex = i;
            }
            alert(document.getElementById('ddStream').selectedIndex);
            return false;
        }

<select id="ddStream" onchange="return Change()">
                            <option value="">EEE</option>
                            <option value="">ECE</option>
                            <option value="">CSE</option>
                            <option value="1">E&I</option>
                        </select>

这是基本语法..你提到的其他内容将由你写的逻辑进行。例如,选择你动态添加的最后一个选项,     document.getElementById('ddStream')。selectedIndex = document.getElementById('ddStream')。options.length -1

答案 2 :(得分:0)

selected =“selected”选择您想要的选项,只需将其放在第3个选项标签

E.g。

<select id="myid"> 
  <option value="1">1 </option>
  <option value="2">2 </option>
  <option value="3" selected="selected">3 </option>
  <option value="4">4 </option>
</select>
相关问题