2016-12-27 1 views
0

이것은 내가 지금까지 시도한 것이다. 어떻게 배열 만 변수 값을 업데이트 못하고Array에서 특정 변수 값을 업데이트하는 방법은 무엇입니까?

내가 +1하여 배열에서 중복 항목을 발견 할 때마다 수량 변수 값을 늘려야 할
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 
    } 
}); 

+0

:

var index = allVals.indexOf(jQuery(this).val()); 

그런 다음, 당신은 단지 = 운영자에 의해 값을 갱신 "] '). val()'? –

+0

그것은 나에게 선택된 체크 박스 수량 필드 값을 줄 것이다 .... 그것은 1,2,3 등 – Uidev123

답변

0

편집 :

가 좋아, 이제 네가 원하는 걸 이해할 것 같아. 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 

이지만 단지 내가 더 나은 솔루션이 입력 고유 ID를 부여하고 id in allVals

와 그 확인하는 생각이

jQuery(this).val() 

을 찾고 있습니다

또한 배열 값은 업데이트되지 않습니다. qty 만 입력란의 값으로 업데이트됩니다. 먼저

+0

과 같은 정수가 될 것이다.하지만 Qty를 찾고있다. – Uidev123

+0

@ Uidev123 업데이트 된 답변을 확인하라. – pixelarbeit

0

, 당신은 indexOf에 의해 값의 인덱스를 찾을 수 : 값이 [이름 = "수량 입력 '(`의 내용

allVals[index] = qty // "qty" after update value 
관련 문제