How can I get values of inputs that have the same class using JQuery each?

前端 未结 4 1110
灰色年华
灰色年华 2021-01-28 07:32

This should be easy, but I can\'t seem to get it right.

var totalHours = 0;
this.$(\".timesheet-daily-input\").each( function () {
    var inputValue = $(this).t         


        
4条回答
  •  粉色の甜心
    2021-01-28 07:42

    It's hard to tell without more code but change

    var inputValue = $(this).text();
    var hours = parseInt(inputValue);
    totalHours += (hours == null) ? 0 : hours;
    

    to

    var inputValue = $(this).val(); // use val
    var hours = parseInt(inputValue, 10); // use 10 as radix
    totalHours += isNaN(hours) ? 0 : hours; // compare to NaN instead of null
    

    Note that parseInt("09") is 0 and that the result of parseInt cannot be null.

提交回复
热议问题