如何选择组合中的第一个值

时间:2016-02-29 09:36:37

标签: javascript

我的html中有一个组合,有时我必须使用javascript将值更改为第一个。

enter code here
   <select  name="myCombo" id="myCombo">
     <option value="1">first option</option>;
     <option value="2">second option</option>;
     <option value="3">third option</option>;
     <option value="4">Other</option>;

  </select> 
   // resetCombo('myCombo')

使用combo作为参数的javascript函数:

function resetCombo(combo) {
      document.getElementById(combo).value =  document.getElementById(combo).get_items().getItem(0);
 }

但它不起作用。这是正确的方法吗?

3 个答案:

答案 0 :(得分:3)

将您的方法更新为

function resetCombo(combo) {
   document.getElementById(combo).selectedIndex =0;
}

答案 1 :(得分:0)

试试这个,

>>> app.Query(c => c.Button()).First()
Query for Button() gave 1 results.
{
    Id => "buttonGo",
    Description => "android.widget.Button{b312e568 VFED..C. ........ 26,234-454,314 #7f060033 app:id/buttonGo}",
    Rect => {
        Width => 428,
        Height => 80,
        X => 26,
        Y => 328,
        CenterX => 240,
        CenterY => 368
    },
    Label => null,
    Text => "Go!",
    Class => "android.widget.Button",
    Enabled => true
}

答案 2 :(得分:0)

试试这个:

function resetCombo(combo) {
      document.getElementById(combo).value = document.querySelector('#' + combo + ' option:nth-child(1)').value;
 }
document.getElementsByTagName('button')[0].onclick = function() {
  resetCombo('myCombo');
}
<select name="myCombo" id="myCombo">
  <option value="1">first option</option>;
  <option value="2">second option</option>;
  <option value="3">third option</option>;
  <option value="4">Other</option>;

</select>
<button>reset</button>