我有一个标签,costLabel。
我想要做的是根据下拉列表的选定值更改此标签的值。
这是我的HTML:
<table> <tr> <td width="343">Package*</td> <td colspan="4"> <select class="purple" name="package"> <option value="standard">Standard - €55 Monthly</option> <option value="standardAnn">Standard - €49 Monthly</option> <option value="premium">Premium - €99 Monthly</option> <option value="premiumAnn" selected="selected">Premium - €89 Monthly</option> <option value="platinum">Platinum - €149 Monthly</option> <option value="platinumAnn">Platinum - €134 Monthly</option> </select> </td> <tr> <td width="343"> <p>We bills quarterly/annually in advance</p> <p>See <a href="#dialog" name="modal">Pricing</a> for more details</p> </td> <td colspan="4"><label id="costlabel" name="costlabel">Total Cost:</label></td> <tr> </table>
进入成本标签的值是
>标准=“€165季度”
> StandardAnn =“€588每年”
> Premium =“€297 Quarterly”
> PremiumAnn =“€1068每年”
>铂金=“€447季度”
> PlatinumAnn =“€1608每年”
我确实有以下代码,它根据下拉菜单计算成本,但是注册表已经改为更简单(即,折扣选择不会消失),并且我在调整jQuery时遇到了一些麻烦。有人可以帮忙吗?
$(function() { $("#discountselection,select[name=package],input[name=discount]"). change(function() { var selected_value = $("#discountselection").val(),discount = [12,24,36],package = $("select[name=package]").val(),package_prices = {'standard': 49,'premium': 85,'platinum': 134 },cost = package_prices[package] * discount[selected_value-1]; $("#costlabel").val(cost); }); });
解决方法
对于您的HTML结构,我似乎有一个盲点,但我认为这是您正在寻找的。它应该从select输入中找到当前选择的选项,将其文本分配给newVal变量,然后将该变量应用于#costLabel标签的value属性:
jQuery的
$(document).ready( function() { $('select[name=package]').change( function(){ var newText = $('option:selected',this).text(); $('#costLabel').text('Total price: ' + newText); } ); } );
HTML:
<form name="thisForm" id="thisForm" action="#" method="post"> <fieldset> <select name="package" id="package"> <option value="standard">Standard - €55 Monthly</option> <option value="standardAnn">Standard - €49 Monthly</option> <option value="premium">Premium - €99 Monthly</option> <option value="premiumAnn" selected="selected">Premium - €89 Monthly</option> <option value="platinum">Platinum - €149 Monthly</option> <option value="platinumAnn">Platinum - €134 Monthly</option> </select> </fieldset> <fieldset> <label id="costLabel" name="costLabel">Total price: </label> </fieldset> </form>
上述工作演示如下:JS Bin