如何使用javascript阅读下拉文本

时间:2015-06-29 04:59:34

标签: javascript jquery html

对于业余问题我很抱歉,但我仍然习惯于习惯JS。当我在html中有多个类时,如何使用java脚本从下拉列表中读取文本。任何帮助将不胜感激。感谢

HTML

 <div class="field">

 <select id="addition_membership_type" class="w230" name="booking.additional_frequentFlyerMembershipType" style="display: none;"   aria-disabled="false">
    <option class="hide-from-ui-list" value=""> … </option>
    <option value="IBP"> … </option>
    <option value="JET"> … </option>

</select>
<a id="addition_membership_type-button" class="ui-selectmenu ui-widget ui-state-default ui-selectmenu-dropdown w230 ui-corner-all" aria-owns="addition_membership_type-menu" aria-haspopup="true" tabindex="0" href="#" role="button" style="width: 230px;" aria-disabled="false">
    <span class="ui-selectmenu-status">

        Jet Privilege

    </span>

的Javascript

<script>




$(document).ready (function()
 {
 var dropdown = $(".field", "#addition_membership_type").text();

if (dropDown == "JET")
{
 window.alert();
 }
 });
 </script> 

3 个答案:

答案 0 :(得分:1)

我在这里创造了一个小提琴 https://jsfiddle.net/98b6jr0b/

这给出了所选下拉列表的索引和值。

<select id="mySelect">
  <option value="test1">test1</option>
  <option value="test2" selected="selected">test2</option>
 <option value="test3">test3</option>
</select>

js代码

var x = document.getElementById("mySelect").selectedIndex;
var y = document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);

答案 1 :(得分:0)

应该使用val():

<script>

$(document).ready (function()
{
var dropdown = $(".w230").val();

if (dropDown == "JET")
{
window.alert("JET");
}
});
</script> 

答案 2 :(得分:0)

试试这个..

<script>
  $('#addition_membership_type').on('change',function(event){
alert('Selected Value: '+$('#addition_membership_type').val());
alert('Selected Text: '+$('#addition_membership_type option:selected').text());  
  });
</script>

Working DEMO Here