jQuery .each()函数通过索引访问其他选择器 [英] jQuery .each() function accessing other selector via indexes

查看:158
本文介绍了jQuery .each()函数通过索引访问其他选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类似的代码:

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

However it doesn't work and I retrieve the following error TypeError: theQuantities[i].val is not a function

我认为我需要使用.each,但是我也需要引用theValuestheDisounts.

I think I need to use .each however I need to reference theValues and theDisounts as well.

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

What is the correct way to access other selectors with the same index within the .each() function?

类似这样的东西也可以访问theValuestheDisounts:

Something like this that can also access the theValues and theDisounts as well:

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)它将返回位置i处的jQuery对象,然后可以使用jQuery的val()

When calling a jquery array object using brackets (like theQuantities[i]) you're getting back an HTML node element, not a jQuery object. try using theQuantities.eq(i) which will return the jQuery object in position i, then you can use jQuery's val()

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

for more on eq, see jQuery documentation: https://api.jquery.com/eq/

给出一个代表一组DOM元素的jQuery对象,.eq()方法从该元素集中的一个元素构造一个新的jQuery对象.提供的索引标识该元素在集合中的位置.

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.

这篇关于jQuery .each()函数通过索引访问其他选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆