如何使用JQuery获取具有相同类的输入值?

这应该很容易,但我似乎无法做到正确。

var totalHours = 0; this.$(".timesheet-daily-input").each( function () { var inputValue = $(this).text(); var hours = parseInt(inputValue); totalHours += (hours == null) ? 0 : hours; }); 

我已经尝试了$(this).val()$(this).html() ,但无法从输入字段中获取值。

编辑:道歉,我知道我忘了提一些东西。 “这个。” 在每个循环的开始是因为我使用骨干的模型和集合。 第一个“这个”。 指的是具体的模型。 它循环了适当的次数,我只是无法获得价值。

摆脱this.$(".timesheet-daily-input")并使用$(this).val()来获取输入的值。

 var inputValue = $(this).val(); 

没有更多的代码但很难改变

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

 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 

请注意, parseInt("09")0并且parseInt的结果不能为null

无法找到确切的解决方案,但通过选择父div并循环遍历其子元素来改变我的方法。

 var totalHours = 0; this.$("#timesheet-daily-entry-fields-container").children().each(function () { var inputValue = $(this).val(); var hours = parseInt(inputValue); totalHours += isNaN(hours) ? 0 : hours; });