代码之家  ›  专栏  ›  技术社区  ›  Paul D. Waite

如何通过javascript在选择中获取当前选中的选项?

  •  53
  • Paul D. Waite  · 技术社区  · 14 年前

    你怎么得到 目前 挑选出来的 <option> A的 <select> 元素通过javascript?

    4 回复  |  直到 7 年前
        1
  •  91
  •   Pat    10 年前

    这将为您做到:

    var yourSelect = document.getElementById( "your-select-id" );
    alert( yourSelect.options[ yourSelect.selectedIndex ].value )
    
        2
  •  18
  •   Andy E    14 年前

    这个 .selectedIndex select 对象有一个索引;可以使用该索引对 .options 数组。

        3
  •  2
  •   Paul Roub jim    8 年前
    var payeeCountry = document.getElementById( "payeeCountry" );
    alert( payeeCountry.options[ yourSelect.selectedIndex ].value );
    
        4
  •  1
  •   Finesse    7 年前

    使用 selectedOptions 财产:

    var yourSelect = document.getElementById("your-select-id");
    alert(yourSelect.selectedOptions[0].value);
    

    works 在除Internet Explorer以外的所有浏览器中。