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

如何使用JQuery分别获取具有相同类的输入值?,jquery,html,class,backbone.js,selector,Jquery,Html,Class,Backbone.js,Selector,这应该很容易,但我似乎做得不对 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(),

这应该很容易,但我似乎做得不对

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.”是因为我使用的是主干网的模型和集合。第一个“this.”指的是有问题的特定模型。它循环了正确的次数,我就是不能得到值。

去掉
这个。
$(“.timesheet daily input”)
之前,使用
$(this.val()
来获得输入值。

去掉
这个。
$(“.timesheet daily input”)
之前,使用
$(this.val())
获取输入值。

如果没有更多的代码,就很难判断是否需要更改

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


请注意,parseInt的结果不能是
null

,如果没有更多的代码,就很难判断结果是否正确

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


请注意,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;
});

找不到确切的解决方案,但通过选择父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;
});

这是什么角色?
this.$(“.timesheet daily input”)。
这是错误吗?请删除$之前的“this”。如果这是窗户,也许可以,但它不干净。然后使用val()获取值。您的问题是什么?什么不起作用?它循环了适当的次数,但没有得到任何值。如果我将
警报(inputValue)
放在
var inputValue
行之后,警报框将不包含任何内容。这是什么作用?
this.$(“.timesheet daily input”)。
这是错误吗?请删除$之前的“this”。如果这是窗户,也许可以,但它不干净。然后使用val()获取值。您的问题是什么?什么不起作用?它循环了适当的次数,但没有得到任何值。如果我将
警报(inputValue)
放在
var inputValue
行之后,警报框将不包含任何内容。开头的“this.”指的是我正在使用的特定型号。我没有提到我正在使用主干网的模型和集合。如果我在开始时没有包含“this.”,它会循环遍历我的所有输入,而不是特定模型的输入。开始时的“this.”指的是我正在使用的特定模型。我没有提到我正在使用主干网的模型和集合。如果我不包括“this.”在开始时,它会遍历我的所有输入,而不是特定模型的输入。