jQuery .each() 函数通过索引访问其他选择器

jQuery .each() function accessing other selector via indexes

我有一些这样的代码:

var theQuantities = $('#' + theWindowID + '_form INPUT[name=f\[invoices_items\]\[quantity\]\[\]]');
var theValues = $('#' + theWindowID + '_form INPUT[name=f\[invoices_items\]\[value\]\[\]]');
var theDiscounts = $('#' + theWindowID + '_form INPUT[name=f\[invoices_items\]\[discount\]\[\]]');
var theInvoiceTotalCell = $('#' + theWindowID + '_TDinvoice_total');
var invoiceTotal = 0;
for (var i = 0; i < theQuantities.length; i++) {
    if ($.isNumeric(theQuantities[i].val()) && $.isNumeric(theValues[i].val()) && $.isNumeric(theDiscounts[i].val())) {
        $('#' + theWindowID + '_' + theRowIDs[i].val() + '_TDinvoice_items_subtotal').html('$ ' + parseFloat((theQuantities[i].val() * theValues[i].val()) - theDiscounts[i].val()).toFixed(2));
        var theSubTotal = parseFloat((theQuantities[i].val() * theValues[i].val()) - theDiscounts[i].val()).toFixed(2);
        invoiceTotal += parseFloat(theSubTotal)
    }
}

但是它不起作用,我检索到以下错误 TypeError: theQuantities[i].val is not a function

我想我需要使用 .each 但是我还需要引用 theValuestheDisounts

在 .each() 函数中访问具有相同索引的其他选择器的正确方法是什么?

这样的东西也可以访问 theValuestheDisounts

theQuantities.each(function(index, item) {
    if ($.isNumeric(this.val()) && $.isNumeric(theValues[index].val()) && $.isNumeric(theDiscounts[index].val())) {
      $('#'+theWindowID+'_'+theRowIDs[index].val()+'_TDinvoice_items_subtotal').html('$ ' + parseFloat((this.val() * theValues[i].val()) - theDiscounts[i].val()).toFixed(2));
      var theSubTotal = parseFloat((this.val() * theValues[i].val()) - theDiscounts[i].val()).toFixed(2);
      invoiceTotal += parseFloat(theSubTotal);
    }
  });

当使用方括号(如 theQuantities[i])调用 jquery 数组对象时,您返回的是 HTML 节点元素,而不是 jQuery 对象。 尝试使用 theQuantities.eq(i) 这将 return 位置 i 的 jQuery 对象,然后你可以使用 jQuery 的 val()

有关 eq 的更多信息,请参阅 jQuery 文档:https://api.jquery.com/eq/

Given a jQuery object that represents a set of DOM elements, the .eq() method constructs a new jQuery object from one element within that set. The supplied index identifies the position of this element in the set.

你可以试试这个

var theSubTotal = parseFloat(($(theQuantities[i]).val() * $(theValues[i]).val()) - $(theDiscounts[i]).val()).toFixed(2);