【问题标题】:How to sum all data from selected items in a box when clicked?单击时如何汇总框中所选项目的所有数据?
【发布时间】:2017-02-03 13:38:05
【问题描述】:

我想要一个 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 在结果列表中,我想获得所选产品的总和和产品名称列表。我认为使用数据属性会更容易,因为它可以以不同的方式访问。
  • 好的,您有两种选择产品的方式 1. 带有单选按钮,2. 带有下拉菜单 您将使用的任何其他方法,例如:复选框、文本框多选下拉菜单以及其他.......
  • @RajeshBaskaran 正确。我只有单选按钮和下拉菜单。没有别的了。

标签: jquery attributes sum


【解决方案1】:

试试这个代码,它对我来说很好。

<div id="selectedProductList">
<ul>

</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">

脚本

var sum=0;
$('.websiteProductList').change(function() {

var price=0;var productName='';

$(this).each(function() { var parentlocal=$(this);

    if(parentlocal.find("option:selected").length>0){
     price=parentlocal.find("option:selected").data('price');
     productName=parentlocal.find("option:selected").data('name');
     parentlocal.find("option:selected").attr('disabled', true);
    sum = parseFloat(sum+price);
    $('#selectedProductList > ul').append('<li>' + productName + '</li>');
    }
});
var radio_price=$(this).data('price');
var radio_productName=$(this).data('name');
if(radio_price){
         price=radio_price;
     productName=radio_productName;
    sum = parseFloat(sum+price);
    $('#selectedProductList > ul').append('<li>' + productName + '</li>');
         }
$('#priceTotal').text(parseFloat(sum).toFixed(2));
});

如果您发现任何困难,请回复。

【讨论】:

  • 哇!你很棒。这太棒了。还有机会避免双重价值吗?所以,我有一个单选按钮来选择产品和一个单选按钮来删除。当我单击要删除的单选按钮时,该值也列为 li 元素。如果从列表中删除值并求和,那就太好了。与选择字段相同。点击后,它被停用,但无法撤消错误条目。
  • 您是如何取消选择单选按钮的。您有 2 个不同的单选按钮名称,一旦选择,您就无法取消选择单选按钮。
猜你喜欢
  • 1970-01-01
  • 2012-04-13
  • 2018-01-10
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多