在jQuery中多次向值添加20%

Add 20% to values multiple times in jQuery

本文关键字:添加 jQuery      更新时间:2023-09-26

我正试图在同一行的字段的KeyUp上向span类添加20%。我一直在加20%,我也不认为这些行是单独的。我有以下内容:

jQuery代码:

$(".account-form .price input[type='text']").on('keyup',function(){
        $(".account-form .price").each(function(){
            var value = $(this).find("input[type='text']").val();
        });
        $(".vatPrice").html(value);
    });

html

<ul>
    <li class="price"><label><span>Price row 1</span><input value="20.00" type="text" name="price1"><span class="vatPrice">£0.00</span></label></li>
    <li class="price"><label><span>Price row 2</span><input value="40.00" type="text" name="price2"><span class="vatPrice">£0.00</span></label></li>
    <li class="price"><label><span>Price row 3</span><input value="80.00" type="text" name="price3"><span class="vatPrice">£0.00</span></label></li>
    <li class="price"><label><span>Price row 4</span><input value="120.00" type="text" name="price4"><span class="vatPrice">£0.00</span></label></li>
</ul>

尝试

$(".account-form .price input[type='text']").on('keyup', function () {
    var value = parseFloat(this.value) || 0;
    $(this).next(".vatPrice").html('£' + (value * 1.2).toFixed(2));
});

演示:Fiddle

试试这个,

$(".account-form .price input[type='text']").on('keyup',function(){           
   var value = (parseFloat($(this).val()) * 20) /100;           
   $(this).next('span').text('$' + ((isNaN(value)) ? 0 : value).toFixed(2));
 });

演示