开发者

Obtaining the value of a select dropdown from within a function

I have a method of an object called update that generates an integer, and if it's different than the value of the region variable, assigns it to region. If the integer is the same as region then the user selects a value from a <select> dropdown menu.

I'm trying to figure out how to get the value of the <select> into the function.

<form>
<select name="selectRegion" id="selectRegion" onchange="alert(regionLookUp[event.target.value]); ">
<option value="8" selected="selected">Select a region</option>
<option value="1">Northeast</option>
<option value="2">Southeast</option>
<option value="3">North Central</option>
<option value="4">South Central</option>
<option value="5">Plains</option>
<option value="6">Northwest</option>
<option value="7">Southwest</option>

</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.floo开发者_如何转开发r(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;
 };


Unless you are using prompt, conform or alert, requesting user input will always be asynchronous. With this in mind, you can treat this interaction just as you would an AJAX request so that the event handler becomes synonymous with the AJAX response handler.

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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜