views:

2470

answers:

2

Hi I've spent some time searching around for this, but can't seem to get it all worked out.

I have a shipping drop down and I want to split off the type of shipping and the amount selected and put them into two separate hidden fields. (I am using text fields at the moment for easier testing)

For example- if they choose from the first drop down Overnight Delivery - 14.00 OnChange I want to set the value of my shippingtype to "Overnight Delivery" and my shipping field value to "14.00"

I already have the location working (if user selects pickup, a new drop down appears and whatever you select also sets the value of my shippinglocation field)

Here's what I have so far...

<html>
<head>

<script type="text/javascript">
function SetShipping (objDropDown)
{
    var objHidden = document.getElementById("shipping");
    objHidden.value = objDropDown.value; 
}


function setShippingtype (objDropDown)
{
    var objHidden = document.getElementById("shippingtype");
    objHidden.value = objDropDown.value; 
}


function setShippinglocation (objDropDown)
{
    var objHidden = document.getElementById("shippinglocation");
    objHidden.value = objDropDown.value; 
}

function showEntry(obj,optionValue)
{
 //hide all entry selections onchange
 document.getElementById("pickup").style.display="none";
 if(obj.value=="pickup")
 {
  document.getElementById(optionValue).style.display="inline"; 
 }  
}



</script>
</head>
<body>
<form id="myform">

    <br />
    shippingselectbox
    <br />
    <select onchange="showEntry(this,this.value);setShippingtype(this);" name="shippingselectbox">
      <option value="">Shipping Options</option>
      <option value="pickup">Pickup - no charge</option>
      <option value="UPS">UPS Standard Shipping - 3.00</option>
      <option value="Overnight">Overnight Delivery - 14.00</option>

  </select>
    <br />
    <br />
    pickup 
  <span id="pickup" style="display:none;">
<select name="pickup" onchange="setShippinglocation(this)">
      <option>Please Choose a Location</option>
      <option value="Billings">Billings, MT</option>
      <option value="Livingston">Livingston, MT</option>
      <option value="Miles City">Miles City, MT</option>
      <option value="Cody">Cody, WY</option>
      <option value="Sheridan">Sheridan, WY</option>
  </select>
  </span>


    <br />
    <br />
<br />
 shipping (set hidden form tag - shipping amount for paypal)<br />
    <input name="shipping" type="text" value=""/>

    <br />
    <br />
<br />
 shippingtype (set hidden form tag - variable for paypal to send to Chris)<br />
    <input name="shippingtype" type="text" value=""/>
  <br />
  <br />

  <br />

 shippinglocation  (set hidden form tag -  variable for paypal to send to Chris)<br />
  <input name="shippinglocation" id="shippinglocation" type="text" value=""/>

</form>
</body>
</html>
A: 

Please format the code you posted, this is possible. Just as you say catch the onChange event. I would serialize the value of the drop down element somehow. A dash or underscore or something, then do a split() on the value and store the appropriate splits in the appropriate hidden input value fields.

Eg using prototype for brevity:

function changeHandler(evt) {
    var selected = evt.target[evt.target.selectedIndex];
    var values = selected.value.split("-");
    $('hidden1').value = values[0];
    $('hidden2').value = values[1];
}
Myles
A: 

I took the liberty of changing your method name from SetShipping to setShipping to keep constant your naming convention. Quite frankly I couldn't be bothered to reformat but I tested so have fun playing around with it.

<html>
<head>

<script type="text/javascript">
function setShipping (objDropDown)
{
    var objHidden = document.getElementById("shipping");
    var type = objDropDown.options[objDropDown.selectedIndex].text.substring(objDropDown.options[objDropDown.selectedIndex].text.indexOf('-')+2, objDropDown.options[objDropDown.selectedIndex].text.length);
    objHidden.value = type; 
}


function setShippingtype (objDropDown)
{
    var objHidden = document.getElementById("shippingtype");
    var type = objDropDown.options[objDropDown.selectedIndex].text.substring(0, objDropDown.options[objDropDown.selectedIndex].text.indexOf('-')-1);
    objHidden.value = type; 
}


function setShippinglocation (objDropDown)
{
    var objHidden = document.getElementById("shippinglocation");
    objHidden.value = objDropDown.value;
}

function showEntry(obj,optionValue)
{
    //hide all entry selections onchange
    document.getElementById("pickup").style.display="none";
    if(obj.value=="pickup")
    {
        document.getElementById(optionValue).style.display="inline"; 
    }  
}
</script>
</head>
<body>
<form id="myform">

<br />
shippingselectbox
<br />
<select onchange="showEntry(this,this.value);setShippingtype(this);setShipping(this);" name="shippingselectbox">
  <option value="">Shipping Options</option>
  <option value="pickup">Pickup - no charge</option>
  <option value="UPS">UPS Standard Shipping - 3.00</option>
  <option value="Overnight">Overnight Delivery - 14.00</option>
</select>
<br />
<br />
pickup 
<span id="pickup" style="display:none;">
<select name="pickup" onchange="setShippinglocation(this)">
  <option>Please Choose a Location</option>
  <option value="Billings">Billings, MT</option>
  <option value="Livingston">Livingston, MT</option>
  <option value="Miles City">Miles City, MT</option>
  <option value="Cody">Cody, WY</option>
  <option value="Sheridan">Sheridan, WY</option>
</select>
</span>
<br />
<br />
<br />
shipping (set hidden form tag - shipping amount for paypal)<br />
<input name="shipping" id="shipping" type="text" value=""/>
<br />
<br />
<br />
shippingtype (set hidden form tag - variable for paypal to send to Chris)<br />
<input name="shippingtype" id="shippingtype" type="text" value=""/>
<br />
<br />
<br />
shippinglocation  (set hidden form tag -  variable for paypal to send to Chris)<br />
<input name="shippinglocation" id="shippinglocation" type="text" value=""/>
</form>
</body>
</html>
Ambrosia