我有一個表單,用戶可以通過選擇複選框輸入詳細信息並選擇一個項目。jquery - 如果不存在,只添加數組中的項目,並在其數組中更新數量
點擊添加按鈕檢查項目值和用戶輸入值存儲在數組中,我傳遞這些值在URL中。
用戶可以添加具有不同名稱和電子郵件的多個項目。假設我有三個項目,所以用戶可以選擇item1並填寫name1和email1並單擊add按鈕,同樣如果他選擇item1和item2並添加name2和email2,他可以添加這些值。
每次點擊我想如果它已經存在,檢查項目ID在陣列我不想添加它,只是增加該項目是已經存在於數組和更新數組新數量的數量
var allVals = []; //array where i am storing selected checkbox value
jQuery("#btnadd").click(function(event) { //on click of this button i am getting all values to construct url and pass this value to multi div from where i will validate my url
var qty;
jQuery(".selectitem:checked").each(function() {
qty = jQuery(this).next().find('input[name="quantity"]').val()); //getting quantity of selected checkbox its a input field
if (jQuery.inArray(jQuery(this).val(), allVals) == -1) {
allVals.push(jQuery(this).val() + ',' + qty); //item id not present in array so adding item id,qty in array
} else {
qty = qty + 1;
//here i would like to increase quantity of those items only which already present in array something like this.qty and update my allVal array with this new quantity
}
});
var rslt = allVals.join(';'); jQuery('#multi').val(rslt); //updating multi div value with allVals from here i will make redirect. Just want to pass correct values in that field
});
可以請你的HTML? –
所以問題是更新數量的權利?另外,你將數據添加到數組中的方式效率不高..就像使用這種方法,你不知道哪個項目具有什麼數量 –
是的......只有數量更新......我的代碼是在不同的字符串中添加項目ID ......就像如果我選擇兩次item1它添加在數組item1,1; item1,1像這樣,但我想顯示它item1,2;以這種方式 – Uidev123