为下拉列表项赋值显示在标签中
我有一个下拉列表,我想做的是将值分配给下拉列表中的项目,然后根据选择的项目在标签中显示这些值。
这是我已经拥有的:
<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 style="font-size:14px;">We bill quarterly/annually in advance</p>
<p style="font-size:14px;">See <a href="#dialog" name="modal">Pricing</a> for more details
</p></td>
<td colspan="4"><label id="total" name="total">Total Cost:</label></td>
我的 jQuery 代码目前已将标签的值更改为下拉列表的实际内容,但是我想将以下值分配给下拉列表的内容:
标准 =“165 欧元季度”
StandardAnn =“每年 588 欧元”
保费 =“每季度 297 欧元”
PremiumAnn =“每年 1068 欧元”
白金 =“每季度 447 欧元”
PlatinumAnn =“每年1608欧元”
这样,当用户选择某个套餐时,将向他们显示总价格,因为他们可以按季度预付费用,也可以按年预付费用。尽量避免他们进入付款页面时感到震惊! 这是我的 jQuery:
$(document).ready(
function() {
$('select[name=package]').change(
function(){
var newText = $('option:selected',this).text();
$('#total').text('Total price: ' + newText);
}
);
} );
我在调整代码时遇到了一些麻烦,因为我是 jQuery 的新手,而且我面临着完成这项工作的压力。有人可以帮我吗?
I have a dropdown list, what I would like to do is assign values to the items in the drop down list and then display these values in a label depending on which item is selected..
Here is what I have already:
<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 style="font-size:14px;">We bill quarterly/annually in advance</p>
<p style="font-size:14px;">See <a href="#dialog" name="modal">Pricing</a> for more details
</p></td>
<td colspan="4"><label id="total" name="total">Total Cost:</label></td>
The jQuery code I currently have changes the value of the label to the ACTUAL contents of the drop down list, however I want to assign the following values to the contents of the dropdownlist:
Standard = "€165 Quarterly"
StandardAnn = "€588 Annually"
Premium = "€297 Quarterly"
PremiumAnn = "€1068 Annually"
Platinum = "€447 Quarterly"
PlatinumAnn = "€1608 Annually"
This is so that when the user selects a certain package, the total price will be displayed to them as they either pay quarterly in advance, or annually in advance. Trying to prevent a shock when they get to the payment page!
Here is my jQuery:
$(document).ready(
function() {
$('select[name=package]').change(
function(){
var newText = $('option:selected',this).text();
$('#total').text('Total price: ' + newText);
}
);
} );
I'm having a bit of trouble adapting the code as I'm new to jQuery and I'm under pressure to have this done. Can someone help me, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以制作一个要显示的文本数组,
这不是最佳实践,但您的整个代码不是。这又快又脏。不太可维护。
You could make an array of the text you want to display
not very best practice but your whole code isnt. this is quick and dirty. not so maintainable.
我花了比我预期更长的时间,并且不涉及重写 html(无论如何,我记得做过),但以下内容确实有效:
jQuery:
html:
Live demo: JS Bin
我知道你已经接受了答案,但放弃最后半个小时的尝试记住正则表达式有多么简单似乎是一种浪费有效...=)
It took me a while longer than I expected, and involves no re-writing of your html (that I can remember doing, anyway), but the following does work:
jQuery:
html:
Live demo: JS Bin
I realise that you've already accepted an answer, but it seemed a waste to throw away the last half hour of trying to remember how simple regex works... =)
基于选择字段中显示的值的快速而肮脏的正则表达式应该可以工作:
同样,这不是最佳实践,但我认为它会工作。请注意,如果下拉菜单选项中的价格之前有任何其他数字,这将会中断。
A quick and dirty regex, based on the value displayed in the select field, should work:
Again, this isn't the best practice, but I think it will work. NB that this will break if any other numbers are before the price in the drop down menu options.