【发布时间】:2011-06-06 05:33:34
【问题描述】:
回答: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>
我要做的是发送选择对象,从选项菜单中提取名称和文本/值以及选项标签中的数据。
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;
}
我可以获取名称和选项值,但我似乎无法从选择对象中获取 text="" 或 value=""。我只需要用户选择的选项菜单中的文本/值。我知道我可以将它们放在一个数组中,但这无济于事
var option_user_selection = element.options[ whatever they select ].text
我还需要使用传入的选择引用,因为这就是它在我的其余代码中的设置方式。
稍后,该文本/值将用于提取 XML 文档,该文档将动态填充下一个选择表单。
【问题讨论】:
-
使用 jQuery 你可以做类似
jQuery('#mySelect option[value=myValue]') -
rofl,这是 5 年前的事了……
标签: javascript html forms html-select