0

I have a select box and I'd like to change the value of variable based on the selected options.

   <form>
          <input type="hidden" value="PIONEER" />
          <select name="select" id="select">
            <option>Select Your Pizza</option>
            <option value="6.65">NY, 10&quot;, £6.65</option>
            <option value="8.95">NY, 12&quot;, £8.95</option>
            <option value="11.95">NY, 16&quot;, £11.95</option>
            <option value="3.45">Chicago, 7&quot;, £3.45</option>
            <option value="6.65">Chicago, 10&quot;, £6.65</option>
            <option value="8.95">Chicago, 12&quot;, £8.95</option>
            <option value="11.95">Chicago, 16&quot;, £11.95</option>
            <option value="19.95">Chicago, Beast 24&quot; x 18&quot;, £19.95</option>
          </select>
          </form>

 $(function() { 
   var selected_pizza = "";
var toppingPrice = 0;
$('#select').change(function() {
    selected_pizza = $('#select option:selected').text();

    alert(selected_pizza);
});
if(selected_pizza.indexOf('Chicago,7') != 1 ) {
     toppingPrice =parseFloat(0.70);
}
if(selected_pizza.indexOf('NY,10') != 1) {
     toppingPrice = parseFloat(0.90);
}
if(selected_pizza.indexOf('NY,12') != 1) {
     toppingPrice = parseFloat(1.05);
}
if(selected_pizza.indexOf('NY,16') != 1) {
     toppingPrice = parseFloat(1.30);
}

});

but this doesn't give me the correct values, is there any other way to do that.

Thanks

4
  • Could you post the HTML for this as well please? Commented Aug 17, 2009 at 19:21
  • amir, are you still going with this page! Commented Aug 17, 2009 at 19:22
  • does the alert show the correct value that is selected? Commented Aug 17, 2009 at 19:25
  • yes, the alert shows the correct value Commented Aug 17, 2009 at 19:26

3 Answers 3

1

Try this:

var selected_pizza;
$('#select').change(function() {
   selected_pizza = $(this).val();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your indexOf calls have don't have spaces after the commas.

2 Comments

still the same problem,for example if I choose the second option instead of adding 0.90 it adds 1.30. Thanks
i think you want indexOf(...) == 0
0

The text in the option elements contains a space after each comma. The strings you pass to indexOf don't.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.