gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:22:03 24 4
gpt4 key购买 nike

我有一些像这样的代码:

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) 它将返回位置 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.

关于javascript - jQuery .each() 函数通过索引访问其他选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39740918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com