如何仅使用静态选项获取选项文本而不是值

时间:2014-11-24 20:47:36

标签: javascript html angularjs

当我只有硬编码选项时,如何提取选项文本(选择)?

<select ng-change="addLanguage( )" ng-model="language">
<option value=af>Afrikaans</option>
...
(374563 options more)
...
</select>

那么我怎样才能得到&#34;南非荷兰语&#34;在我的addLanguage()?请注意,html中的选项很难,没有json。

2 个答案:

答案 0 :(得分:1)

为您的选择标记添加ID:

<select id="taal" ng-change="addLanguage( )" ng-model="language">
<option value=af>Afrikaans</option>
...
(374563 options more)
...
</select>

然后它会真的很容易:

var elem = document.getElementById("taal");
var taal = elem.options[elem.selectedIndex].text;

答案 1 :(得分:1)

你可以这样做:

<select id="languageSelect" ng-change="addLanguage( )" ng-model="language">
<option value=af>Afrikaans</option>
...
(374563 options more)
...
</select>

<script>
function addLanguage()
{
     window.alert(document.getElementById("languageSelect").options[document.getElementById("languageSelect").selectedIndex].innerHTML); //this will have text in it
}
</script>