Phi*_*hil 55 html javascript forms html-select
答案:var option_user_selection = element.options[element.selectedIndex].text
我正在尝试建立一个填写一个人订单的表单.
<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>
Run Code Online (Sandbox Code Playgroud)
我要做的是发送选择对象,从选项菜单和选项标签中的数据中提取名称和文本/值.
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;
}
Run Code Online (Sandbox Code Playgroud)
我可以获取名称和选项值,但我似乎无法从select对象中获取text =""或value ="".我只需要用户选择的选项菜单中的文本/值.我知道我可以将它们放在一个数组中,但这没有用
var option_user_selection = element.options[ whatever they select ].text
Run Code Online (Sandbox Code Playgroud)
我还需要使用传入的select引用,因为它是在我的其余代码中设置的.
稍后,该文本/值将用于提取将动态填充下一个选择表单的XML文档.
Cha*_*ndu 68
您可以使用:
var option_user_selection = element.options[ element.selectedIndex ].text
Run Code Online (Sandbox Code Playgroud)