【发布时间】:2018-02-28 06:38:57
【问题描述】:
在我的网站上,我有一个选项,用户可以将多个客人添加到预订中并选择他们想要购买的套餐。
选择套餐后,价格会根据选择选项的值自动更新。
出于某种原因 - 第一个下拉菜单(已经在页面上)起作用并更新价格。
但是,当您运行 newMenuItem() 函数时,它为您提供的选择下拉菜单不会更新价格。
这是我的代码
HTML
<h3 class="margin-top-0 margin-bottom-30">Guest Details <small>(Guests you're paying for)</small></h3>
<table id="pricing-list-container">
<tr class="pricing-list-item pattern">
<td>
<div class="fm-input pricing-name"> <label>Package</label>
<select data-placeholder="Select Item">
<option>Select a package </option>
<option value = "100">1</option>
<option value = "200">2</option>
<option value = "300">3</option>
</select>
</div>
<div class="fm-close"><a class="delete" href="#"><i class="fa fa-remove"></i></a></div>
</td>
</tr>
</table>
<a href="#" class="button add-pricing-list-item">Add Guest</a>
<span id = "price">ddd</span>
JS
$(document).ready(function(){
$(function () {
var fields = $('select').change(calculate);
function calculate() {
var price = 0;
fields.each(function () {
price += +$(this).val();
})
$('#price').html(price.toFixed(2));
}
})
function newMenuItem() {
var newElem = $('tr.pricing-list-item.pattern').first().clone();
newElem.find('input').val('');
newElem.appendTo('table#pricing-list-container');
}
if ($("table#pricing-list-container").is('*')) {
$('.add-pricing-list-item').on('click', function(e) {
e.preventDefault();
newMenuItem();
});
// remove ingredient
$(document).on( "click", "#pricing-list-container .delete", function(e) {
e.preventDefault();
$(this).parent().parent().remove();
});
}
});
【问题讨论】:
标签: jquery