开发者

How do I pass a form name into a if else if else loop?

开发者 https://www.devze.com 2022-12-20 21:47 出处:网络
function ship_stuff() { ship_me = document.getElementById(\"shipMethod\").value; alert(ship_me); if(\"ship_me\" == \"priority2\" )
function ship_stuff()
{

ship_me = document.getElementById("shipMethod").value;
alert(ship_me);
if("ship_me" == "priority2" )
{
    document.getElementById("shippingChg").value = 3.20;
}

else if(ship_me == "priority3")
{
    document.getElementById("shippingChg").value = 4.30 ;
}
else if(ship_me == "priority4")
{
    document.getElementById("shippingChg").value = 5.40 ;
}

else if(ship_me == "overnight")
{
    document.getElementById("shippingChg").value = 15.75 ;
}

else if(ship_me == "UPS")
{
    document.getElementById("shippingChg").value = 15.00 ;
}

else  (ship_me == "free")
{
    document.getElementById("shippingChg").value = 0.00 ;
}

The HTML portion of the code:

<tr>
        <th colspan="4" class="label">Shipping Method:
          <select name="shipMethod" id="shipMethod" size="1">
            <option value="">Choose a shipping method</option>
            <option value="priority2">Priority mail $3.20 (4 items or less)</option>
            <option value="priority3">Priority mail $4.30 (6 items or less)</option>
            <option value="priority4">Priority mail $5.40 (8 items or less)</option>
            <option value="overnight">Express $15.75 (4 items or less only)</option>
            <option value="UPS">UPS - 2 day $15.00 (9 - 49 items)</option>
            <option value="free">Free shipping (50+ items only)</option>
          </select>
        </th>
        <td><input name="shippingChg" id="shippingChg" type="text" size="10" maxlength="60" value="" /></td>
      </tr>
      <tr>

My javascript loop is fail开发者_如何学Pythoning when I try and compare my variable to a string. It wont put the option value in to compare as a string. Any ideas how I can fix this?

Thanks for the help!


You're missing an if in the final else clause. If you want it to always run, remove the conditional in parentheses.

Also, try a mapping instead of the if/else chain:

var shippingCharges = {
  "priority2" : 3.20,
  "priority3": 4.30,
  "priority4" : 5.40,
  "overnight": 15.75,
  "UPS": 15.00,
  "free": 0.00
};

document.getElementById("shippingChg").value = shippingCharges[ship_me];


This is suspicious:

if("ship_me" == "priority2" )

Don't you mean:

if(ship_me == "priority2" )   

You may also want to declare ship_me as a var:

var ship_me = document.getElementById("shipMethod").value; 

Explicit casting to a string can be done like so:

ship_me  = String(ship_me);


ship_me = document.getElementById("shipMethod").options[document.getElementById("shipMethod").selectedIndex];

Thats the correct way to get the selected option.


Hey Matt, do yourself a favor and use this code instead:

var shipping_costs = {
    'priority2': 3.2,
    'priority3': 4.3,
    'priority4': 5.4,
    'overnight': 15.75,
    'UPS': 15.0,
    'free': 0.0
};

var method = document.getElementById('shipMethod').value;

document.getElementById("shippingChg").value = shipping_costs[method] || 0.0;

That's all there is to it. Keep in mind though that I mapped the descriptive option <option value="">Choose a shipping method</option> (the user didn't select anything) to the free shipping method.

0

精彩评论

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

关注公众号