2011-01-09 117 views
48

回答选项的文本/值:var option_user_selection = element.options[element.selectedIndex].text掌握JavaScript的

我试图建立在一个人的顺序填写表格。

<form name="food_select"> 
    <select name="maincourse" onchange="firstStep(this)"> 
     <option>- select -</option> 
     <option text="breakfast">breakfast</option> 
     <option text="lunch">lunch</option> 
     <option text="dinner">dinner</option> 
    </select> 
</form> 

我想要做的是发送选择对象,从选项菜单中拉出名称和文本/值和选项标记中的数据。

function firstStep(element) { 
    //Grab information from input element object 
    /and place them in local variables 
    var select_name = element.name; 
    var option_value = element.value; 
} 

我能得到的名称和选项值,但我似乎无法得到从选择对象的文本=“”或值=“”。我只需要用户选择的选项菜单中的文本/值。我知道我可以将它们放置在一个数组,但这并不能帮助

var option_user_selection = element.options[ whatever they select ].text 

我还需要使用传入的选择参考,因为这是它是如何建立在我的代码的其余部分。

稍后,将使用该文本/值来拉动将动态填充下一个选择表单的XML文档。

+3

http://stackoverflow.com/questions/1085801/how-to-get-the -selected-value-of-dropdownlist-using-javascript – trante 2014-02-04 18:58:03

+0

使用jQuery你可以做类似`jQuery('#mySelect option [value = myValue]') – 2015-07-15 18:58:52

+3

[Retrieving the text of the selected