最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calculate the sum of products using a td attribute - Stack Overflow

programmeradmin0浏览0评论

I have this table :

<table>
    <thead>
        <tr>
            <th>Quantity</th>
            <th>&nbsp;</th>
            <th>Price</th>
            <th>Sum</th>
        </tr></thead>
    <tbody>
        <tr class="sum">
            <td><input class="qty" type="text" value="1" /></td>
            <td>German format: </td>
            <td data_price="1375.5">1.375,50 &euro;</td>
            <td></td>
        </tr>
        <tr class="sum">
            <td><input class="qty" type="text" value="1" /></td>
            <td>English format:</td>
            <td data_price="1375.5">&euro;1,375.50</td>
            <td></td>
        </tr>
        <tr class="sum">
          <td><input name="text" type="text" class="qty" value="1" /></td>
          <td>French format:</td>
          <td data_price="1375.5">1 375,50 &euro;</td>
          <td></td>
        </tr>
        <tr class="sum">
          <td><input name="text2" type="text" class="qty" value="1" /></td>
          <td>Italian format:</td>
          <td data_price="1375.5">&euro;1,375.50</td>
          <td></td>
        </tr>
        <tr class="sum">
             <td><input class="qty" type="text" value="1" /></td>
             <td>Spanish format:</td>
            <td data_price="1375.5">&euro; 1.375,50</td>
            <td></td>
        </tr>
        <tr>
            <td colspan="3">Total</td>
            <td id="total"></td>
        </tr>
    </tbody>
</table>

and I want to use the value of attribute "data_price" to calculate the SUM like in this link : /

I want to use only the attribute "data_price" in calculation and not the .

Please help, I'm still beginner in jquery :)

I have this table :

<table>
    <thead>
        <tr>
            <th>Quantity</th>
            <th>&nbsp;</th>
            <th>Price</th>
            <th>Sum</th>
        </tr></thead>
    <tbody>
        <tr class="sum">
            <td><input class="qty" type="text" value="1" /></td>
            <td>German format: </td>
            <td data_price="1375.5">1.375,50 &euro;</td>
            <td></td>
        </tr>
        <tr class="sum">
            <td><input class="qty" type="text" value="1" /></td>
            <td>English format:</td>
            <td data_price="1375.5">&euro;1,375.50</td>
            <td></td>
        </tr>
        <tr class="sum">
          <td><input name="text" type="text" class="qty" value="1" /></td>
          <td>French format:</td>
          <td data_price="1375.5">1 375,50 &euro;</td>
          <td></td>
        </tr>
        <tr class="sum">
          <td><input name="text2" type="text" class="qty" value="1" /></td>
          <td>Italian format:</td>
          <td data_price="1375.5">&euro;1,375.50</td>
          <td></td>
        </tr>
        <tr class="sum">
             <td><input class="qty" type="text" value="1" /></td>
             <td>Spanish format:</td>
            <td data_price="1375.5">&euro; 1.375,50</td>
            <td></td>
        </tr>
        <tr>
            <td colspan="3">Total</td>
            <td id="total"></td>
        </tr>
    </tbody>
</table>

and I want to use the value of attribute "data_price" to calculate the SUM like in this link : http://jsfiddle/QmTNZ/77/

I want to use only the attribute "data_price" in calculation and not the .

Please help, I'm still beginner in jquery :)

Share Improve this question edited Jul 18, 2011 at 9:45 Lightness Races in Orbit 385k77 gold badges666 silver badges1.1k bronze badges asked Jul 18, 2011 at 9:40 DaliDali 7,88228 gold badges114 silver badges178 bronze badges 2
  • 3 Shouldn't those data attributes have a dash, not an underscore? – Ben Everard Commented Jul 18, 2011 at 9:43
  • Please stop writing tags and thanks. Thanks! :) – Lightness Races in Orbit Commented Jul 18, 2011 at 9:46
Add a ment  | 

3 Answers 3

Reset to default 6

For your price cells, you should use this format:

<td data-price="1375.5">&euro;1,375.50</td>

i.e. with a hyphen, not underscore

You can then use:

$('td').data('price')

to access its value - see http://api.jquery./data

e.g.

var sum = 0;
$('.sum').each(function() {
    var q = parseFloat($(this).find('.qty').val());
    var p = parseFloat($(this).find('td').eq(2).data('price'));
    sum += q * p;
});
$('#total').text(sum);

Working demo at http://jsfiddle/alnitak/gzYhN/

Try

var sum=0;
$('tbody > tr >  td[data_price]').each(
   function()
   {
      sum += parseFloat(this.attr('data_price'));
   }
);

Try this: http://jsfiddle/ptfKJ/

This example is using your original HTML code.

发布评论

评论列表(0)

  1. 暂无评论