我想要一個div框分別顯示所有選定項目的價格以及所選項目的價格總和。如何在單擊時從一個框中選擇所有項目的數據?
我有這樣的顯示與所選擇的產品包裝盒和價格的總和:
<div id="selectedProductList">
<ul>
<li>selected product…</li>
<li>selected product…</li>
</ul>
</div>
<div id="priceTotal">xyz $</div>
而且在選擇和輸入字段的產品名稱和價格數據:
<select class="websiteProductList" name"product1">
<option data-price="29.99" data-name="Product 1" value="1">Product 1</option>
<option data-price="39.00" data-name="Product 1 with special" value="0">Product1 with special</option>
</select>
<input class="websiteProductList" type="radio" name="product2" data-name="Product 2" data-price="49.99">
<input class="websiteProductList" type="radio" name="product3" data-name="Product 3" data-price="59.99">
我有此代碼:
$('.websiteProductList').change(function() {
$(this).find('option:selected').each(function() {
if ($(this).attr('data-price')) {
price = $(this).data('price');
productName = $(this).data('name');
sum += price;
$('#selectedProductList > ul').append('<li>' + productName + '</li>');
}
});
$('#priceTotal').text(sum);
});
但它沒有工作。不幸的是,我的知識在JavaScript中也非常有限。因爲你可以立即看到它,可能代碼太糟糕了,以至於他永遠無法工作。不幸的是,我不能再走了。有任何想法嗎?
結果是什麼 –
可我知道你在數據屬性中保留價格的原因是什麼?你可以將價格作爲價值知道.....任何原因? –
@RajeshBaskaran 在結果列表中,我想獲得總金額和清單所選產品的產品名稱,我認爲使用數據屬性會更容易,因爲它可能是ac奇怪的是, – user2693715