Javascript - returning concatenates instead of sum of variables from input

前端 未结 6 868
南笙
南笙 2021-01-23 07:12

I am trying to get values from input field using .each() and adding them to show the sum in another input value. But unfortunately in spite of using parseInt() I am

6条回答
  •  南方客
    南方客 (楼主)
    2021-01-23 08:05

    You should declare totalvalue inside focusout function ,because it will always show the total of two input text and pass the input value empty state when sum !!!

    $(document).on("focusout",".value",function() {
        var totalvalue = 0;
        $(".value").each(function() {
            if($(this).val() == "") return;
            totalvalue +=  parseInt($(this).val());
        });
        $("#totalvalue").val(totalvalue);
    });
    

提交回复
热议问题