【发布时间】:2014-03-27 01:26:44
【问题描述】:
我真的必须开始学习我的 javascript 技能了。我正在尝试,我认为使用 jquery 的简单任务,根据选择的option 为变量设置某个值。
<select>
<option>Choose size</option>
<option id="size1">1</option>
<option id="size2">2</option>
<option id="size3">3</option>
<option id="size4">4</option>
</select>
<input type="text" id="showPrice"/>
jQuery:
var thisPrice;
$('#size1').each(function(){
if(this.selected)
thisPrice = 2;
});
$('#size2').each(function(){
if(this.selected)
thisPrice = 4;
});
$('#size3').each(function(){
if(this.selected)
thisPrice = 5;
});
$('#size4').each(function(){
if(this.selected)
thisPrice = 6;
});
$('#showPrice').val(thisPrice);
我怎样才能让它工作?
【问题讨论】:
-
使用选项值并获取选择值。简单
-
这是一件简单的事情,如果您只需要在其他时间获取代码中的值,我建议避免使用事件处理程序,就像许多答案一样。