문제

I have some jquery taking a string value from a data attribute, and then increasing the value based on the value of value. I am expecting it to increment the value each time and write the new total to the data attribute, however it does not work as expected.

JSfiddle

The JQ

$("#btn").click( function() {
    discount = $("#thisid").data("discount_tendered");
    // alert(discount);
    value = $("#value").text();
    valueDec = parseFloat(value);
    // alert(value);
    newDiscountVal = discount += valueDec;
    // alert(newDiscountVal);
    $("#thisid").attr("data-discount_tendered", newDiscountVal);
});
도움이 되었습니까?

해결책

JSfiddle

$(function() {
 $('a#btn').click(function() { 
 var discount = parseFloat($("#discount").data("discount"));
 var increment = parseFloat($("#increment").text());
 var newDiscountVal = (discount + increment).toFixed(2);
 $("span#discount").text(newDiscountVal);
 $("#discount").data("discount", newDiscountVal);
    });

});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top