Jquery 如何更新数组中的特定变量值?
这就是我到目前为止所尝试的。不了解如何仅更新数组中的变量值Jquery 如何更新数组中的特定变量值?,jquery,Jquery,这就是我到目前为止所尝试的。不了解如何仅更新数组中的变量值 allVals = []; var qty; jQuery(".big:checked").each(function() { qty = parseInt(jQuery(this).parents().nextAll().children().find('input[name="quantity"]').val()); if (jQuery.inArray(jQuery(this).val(), allVals) ==
allVals = [];
var qty;
jQuery(".big:checked").each(function() {
qty = parseInt(jQuery(this).parents().nextAll().children().find('input[name="quantity"]').val());
if (jQuery.inArray(jQuery(this).val(), allVals) == -1) {
allVals.push(jQuery(this).val() + ',' + qty + ',' + 'custcol_name_of_attendees|' + nameatt + '||custcol_email_of_attendees|' + emattendee + '||custcol_event_name|' + eventname + '| |custcol_event_location|' + eventlocation); //not present in array
} else {
qty = parseInt(jQuery(this).parents().nextAll().children().find('input[name="quantity"]').val()) + 1;
// update only qty value in allVals array
}
});
每次在数组中发现重复项时,我想将qty变量值增加+1
好吧,现在我想我明白你想要什么了。Qty不是id的最佳名称。请尝试以下操作:
allVals = [];
jQuery(".big:checked").each(function() {
var elements = jQuery(this).parents().nextAll().children().find('input[name="quantity"]');
jQuery(elements).each(function() {
var qty = parseInt(this.val());
var id = this.attr('id');
if (qty in allVals) {
allVals[qty] += 1;
} else {
allVals[qty] = 1;
}
});
});
===
您的代码永远不会在数组中找到任何内容。你在阵列中的推进是什么
jQuery(this).val() + ',' + qty + ',' + 'custcol_name_of_attendees|' + nameatt + '||custcol_email_of_attendees|' + emattendee + '||custcol_event_name|' + eventname + '| |custcol_event_location|' + eventlocation
但你只是在寻找这个
jQuery(this).val()
我认为更好的解决方案是为您的输入提供一个唯一的id,并在allVals中使用id检查该id
此外,数组值永远不会更新。只有
数量
通过输入字段的值更新。首先,通过索引
查找值索引:
var index = allVals.indexOf(jQuery(this).val());
然后,您只能通过=
运算符更新值:
allVals[index] = qty // "qty" after update value
('input[name=“quantity”]').val()中的值是多少?它将给我选中的复选框数量字段值…它将是整数,如1,2,3等,但我正在搜索数量