如何基于选择选项更改变量的值: [英] how to change the value of a variable based on a select option:selected
本文介绍了如何基于选择选项更改变量的值:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个选择框,我想根据所选选项更改变量的值.
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", £6.65</option>
<option value="8.95">NY, 12", £8.95</option>
<option value="11.95">NY, 16", £11.95</option>
<option value="3.45">Chicago, 7", £3.45</option>
<option value="6.65">Chicago, 10", £6.65</option>
<option value="8.95">Chicago, 12", £8.95</option>
<option value="11.95">Chicago, 16", £11.95</option>
<option value="19.95">Chicago, Beast 24" x 18", £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.
谢谢
推荐答案
尝试一下:
var selected_pizza;
$('#select').change(function() {
selected_pizza = $(this).val();
}
这篇关于如何基于选择选项更改变量的值:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文