从函数内部获取选择下拉列表的值
问题描述:
我有一个对象名为update
的方法生成一个整数,如果它不同于region
变量的值,则将其分配给region
。如果整数与region
相同,则用户从<select>
下拉菜单中选择一个值。从函数内部获取选择下拉列表的值
我想弄清楚如何获得<select>
的值到函数中。
<form>
选择一个区域 东北 东南亚 北环 中南 中原 西北 西南
//Destination object
var destination= function(spec) {
spec= spec||{};
var that= {};
that.update= function(newDest) {
function roll() {
return Math.floor(Math.random()*6)+Math.floor(Math.random()*6)+Math.floor(Math.random()*2)*11;
};
newDest= newDest||{};
//newDest is event
newRegion=newDest.target.value;
//newDest is empty object
newRegion= newDest.region||codes[roll()][0];
if (spec.region=== newRegion) {
//ask user for new region
//how do i use event handlers here?
};
//set new region
spec.region= newRegion;
//set new city
spec.city= newDest.city||codes[roll()][newRegion];
};
return that;
};
答
除非你使用prompt
,conform
或alert
,请求用户输入将始终是异步的。考虑到这一点,您可以像处理AJAX请求一样处理这种交互,以便事件处理程序变成AJAX响应处理程序的同义词。
if (spec.region === newRegion) {
// 1. Display a <select> in the current context or
// launch some sort of dialog containing the <select>.
// 2. Attach an event handler to that <select> that is closed
// over in this scope, or is a method of `destination` and
// update `spec.region` and `spec.city`
} else {
//set new region
spec.region= newRegion;
//set new city
spec.city= newDest.city||codes[roll()][newRegion];
}