使用jQuery在页面上显示输入的总和

使用jQuery在页面上显示输入的总和,jquery,Jquery,我在jQuery方面的经验有限,因此非常感谢您的帮助或建议 我试图得到一个输入的总和来显示,因为它们被添加到一个页面上 我设置了一个JSFIDLE来完成最初的工作: 下面是来自JSFIDLE的代码: Shipping: <input type="text" class="input-small fee" name="ship_fee" id="ship-fee"> Other: <input type="text" class="input-small fee" name="

我在jQuery方面的经验有限,因此非常感谢您的帮助或建议

我试图得到一个输入的总和来显示,因为它们被添加到一个页面上

我设置了一个JSFIDLE来完成最初的工作:

下面是来自JSFIDLE的代码:

Shipping: <input type="text" class="input-small fee" name="ship_fee" id="ship-fee">

Other: <input type="text" class="input-small fee" name="other_fee" id="other-fee">

<div>
    $ <span id="total" data-total="<?=$total?>"><p></p></span>
</div>


$('#fee').change(function () {
    var total = $('.total').val();
    $("p").text(total);
});
配送:
其他:

$费不是一个id,而是一个类。改用
.fee
。同样,total是一个id,而不是一个类,因此请改用
#total

//use the class-selector .fe because fee is the class attribute
var $fees = $('.fee').change(function(){
    var total = 0;
    //iterate through all the fee elements and find the total
    $fees.each(function(){
        total += (parseFloat($.trim(this.value)) ||0)
    })
    $( "p" ).text( total.toFixed(2) );
});    

演示:

您可能是指
$('.fee')。更改
?使用点选择类,而使用#选择ID,则选择不存在的ID