br*********@yahoo.com wrote:
I am hoping for a code example of how to do this, and hopefully it will
help me to see an easy way to code what seems to be a huge monster.
I need to create a form that has two pulldown menues (field names
Option1 and Option2). Each will house a list of the 50 U.S. States.
Users would then be able to choose two states, and get a listing for a
courier used for pickup\deliveries (exp: My order is in Ohio, but I
need it delivered to Maine. Who do I use?)
You can build a lookup table object, using JavaScript object notation.
If you are familiar with hashes, you can also think of this table as a
2-dimensional hash.
var sendFromTo = {
Alaska : {
Alaska : "local mail",
Alabama : "FedEx"
},
Alabama: {
Alaska : "UPS",
Alabama : "pony express"
}
}
Here is an example lookup against this table:
sendFromTo["Alaska"]["Alabama"] //evaluates to "Fedex"
>
I am not as familiar with code as I would like, but it seems I would
need something like this:
If option1 = this, and option2 = this, then the answer (field name
Result) = this
If you use this technique, you don't need a conditional. Instead,
after the user has chosen one value from each menu, submit those two
values to the table, and then display the result.
var from = firstMenu[firstMenu.selectedIndex].text;
var destination = secondMenu[secondMenu.selectedIndex].text;
var msg = sendFromTo[from][destination];
//now display the value of "msg" to the user
Here is the code of a brief, working example of this kind of solution.
You can see it in action at
http://onemorebug.com/meme.washer/co...ect_menus.html
Hope this helps.
<script type="text/javascript">
var sendFromTo = {
Alaska : {
Alaska : "local mail",
Alabama : "FedEx"
},
Alabama: {
Alaska : "UPS",
Alabama : "pony express"
}
}
function getShippingMethod () {
var firstMenu = document.forms[0].elements[0];
var secondMenu = document.forms[0].elements[1];
if (firstMenu.selectedIndex != 0 && secondMenu.selectedIndex !=0) {
// console.warn(1);
var from = firstMenu[firstMenu.selectedIndex].text;
var destination = secondMenu[secondMenu.selectedIndex].text;
var msg = sendFromTo[from][destination];
//alert(msg);
document.getElementById('youChose').innerHTML = msg;
}
}
</script>
<form>
Ship From:
<select onchange="getShippingMethod()">
<option>Select a state</option>
<option>Alaska</option>
<option>Alabama</option>
</select>
<br>
Send To:
<select onchange="getShippingMethod()">
<option>Select a state</option>
<option>Alaska</option>
<option>Alabama</option>
</select>
</form>
<div>Send by method: <span id="youChose">No method selected</span></div>