开发者

Jquery: Get value from dynamic attribute

I’m adding a total amount value to a DIV through a jQuery Ajax call everytime I add a new item to a shopping cart. I need this value to be a part of a difference calculation (payment-totalAmount), but I’m having troubles getting the totalAmount value.

I set the total amount in the function called SetTotalAmount and then I try to get the value from the DIV tag in the submitPayment actionevent:

<script type="text/javascript">
$(document).ready(function(){

    $("#submitPayment").click(function(){
        var paymentAmount = $("#paymentAmount").val();
        var totalAmount = $("#totalTillAmount").val();
        var difference = (paymentAmount-totalAmount);

        $("#paymentTil开发者_开发百科lAmount").html("betalt: "+paymentAmount);
        //$("#totalTillAmount").html("total: "+totalAmount);

        $("#difference").html("Tilbage: "+difference);

        $("#paymentInfo").show('slow');

    });

});


function SetTotalAmount()
{
    $.post("Controller/TillController.php?action=3", 
            function(data)
            {
                $("#totalAmount").html(data);
                $("#totalTillAmount").html(data);
            }
    );
}
</script>


You may need to parse strings before performing calculations:

var paymentAmount = parseFloat($("#paymentAmount").text());
var totalAmount = parseFloat($("#totalTillAmount").text());

Also as #totalTillAmount is a div you may need to use the text function instead of val (which is used for input elements) to read it's contents.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜