在jquery中使用类名获取值

时间:2022-07-26 20:35:33

I'm trying to get the value of an input text field and multiply it by quantity and display on total column.

我正在尝试获取输入文本字段的值并将其乘以数量并显示在总列上。

Here is the jsfiddle

这是jsfiddle

Html

HTML

Price: <input id="price" type="text"/>
<table id="my-table">
<tr>
   <td>Quantity</td>
   <td>Total</td>
</tr>   
<tr>
    <td class="quantity">15,447.84</td>
    <td class="total"></td>
</tr>
<tr>
    <td class="quantity">89.28</td>
    <td class="total"></td>
</tr>
</table>

Srript

Srript

$(document).ready(function () {
    $('#price').on('change', calTotal);
    function calTotal() {
        var p = $('#price').val();
        var q = $('.quantity').text();
        alert(q);
    }
});

var q = $('.quantity').text(); give me output as 15,447.8489.28.

var q = $('。quantity')。text();给我输出为15,447.8489.28。

How can i multiply each quantity by price and print on total column ?

我如何将每个数量乘以价格并打印在总列上?

1 个解决方案

#1


5  

  • Use parseFloat() and .text() to set the values.

    使用parseFloat()和.text()设置值。

  • Also you have two columns for total and quantity each, you need to check values of both the respective columns while assigning the text in total

    此外,您还有两列总数和数量,您需要在分配总计文本时检查相应列的值

  • You need to convert 15,000 to 15000, use replace() for this.

    您需要将15,000转换为15000,请使用replace()。


$('.total').text(function(){
    var q = $(this).prev().text();
    q = q.replace(/,/g, '');                
    p = p.replace(/,/g, '');  
    return (parseFloat(p)*parseFloat(q))
});

Fiddle Demo

小提琴演示

#1


5  

  • Use parseFloat() and .text() to set the values.

    使用parseFloat()和.text()设置值。

  • Also you have two columns for total and quantity each, you need to check values of both the respective columns while assigning the text in total

    此外,您还有两列总数和数量,您需要在分配总计文本时检查相应列的值

  • You need to convert 15,000 to 15000, use replace() for this.

    您需要将15,000转换为15000,请使用replace()。


$('.total').text(function(){
    var q = $(this).prev().text();
    q = q.replace(/,/g, '');                
    p = p.replace(/,/g, '');  
    return (parseFloat(p)*parseFloat(q))
});

Fiddle Demo

小提琴演示