2012-02-11 4 views
4

나는 모바일 애플 리케이션에있어 확인란의 값으로 배열을 채우고 싶습니다.jquery 배열을 채우는 방법

내 코드

if (row.flatdescription == flatdescription) { 
    if (row.receiptno == 0){ 
     items.push('<input type="checkbox" name="code_'+ i +'" id="code_'+ i +'" value="' + row.amount + '" previous="' + row.pastpayments + '" barcode="' + row.barcode + '" todayp="' + row.todaypayments + '"/><label for="code_'+ i +'">' + row.period +'..........'+ row.amount+'</label></br>'); 
    } 
    allbarcode[i] = row.barcode; 
    previouspayments1 = previouspayments1 + row.pastpayments; 
    previouspayments = previouspayments1.toFixed(2); 
    sofeilon1 = sofeilon1 + row.amount; 
    sofeilon = sofeilon1.toFixed(2); 
    total1 = total1 + row.amount - row.pastpayments; 
    total = total1.toFixed(2); 
} 

내 배열 코드

function barcodeTotal() { 
    barcode = []; 
    barcodeamount = []; 
    barcodeprevious = []; 
    $("input:checked").each(function(i) { 
     barcode[i] = $(this).attr("barcode"); 
     barcodeamount[i] = $(this).attr("value"); 
     barcodeprevious[i] = $(this).attr("previous"); 
    }); 
} 

내 목표는 내가 답을 주셔서 감사합니다이

barcode [barcode: barcode, amount: value, previous: previous, .... 

같은 바코드 배열을 채우기 위해입니다

답변

4

당신은 쉽게 배열로 확인 입력 필드를 변환 할 수 있습니다 :

var checkedInputs = $("input:checked") // this contains the raw HTML-Elements 

을 이제 원하는 결과에 따라 당신이 수확 중 하나 값 별도로

var barcodes = checkedInputs.map(function() { return $(this).attr('barcode') }) 
var amounts = checkedInputs.map(function() { return $(this).attr('amount') }) 
var previouss = checkedInputs.map(function() { return $(this).attr('previous') }) 

또는 5 월, twilson과 같은 객체가 제안한 것처럼 더 나은 것일 수도 있습니다.

이 경우 0

당신이이 개체를 참조로thisfunction 전화, 의 내부에서주의 할 , 당신은 $('input:checked') 전화와 일치. twilson을 제안 할 때 var self = this을 저장하면 호출 상자의 this은 대체로 HTMLElement이 아니므로 입력 상자의 실제 값은받지 못하고 값은 표시되지 않습니다.

1

너는 b e는 배열이 아닌 일련의 객체로 제공됩니다.

var self = this; // Important because 'this' may change in scope. 
var barcodes = [] 

$("input:checked").each(function(index, item) { 
    barcodes.push({ 
     barcode: $(self).attr("barcode"), 
     amount: $(self).attr("value"), 
     previous: $(self).attr("previous") 
    }); 
}); 

당신은 다음과 같은 배열 뭔가 종료됩니다 :

  1. [ 
        { barcode: "5049383", amount: "4", previous: "17263742" }, 
        { barcode: "5049389", amount: "1", previous: "1726376" } 
    ] 
    
    가 그렇지 않으면 전역 것이다 함수 내부에 변수 전에 var 키워드를 넣습니다.
  2. var self = this;은 일반적으로 추가 기능을 호출 할 때 우수 사례로, this의 범위가 실행 지점에 따라 상당히 달라질 수 있기 때문에 대개 기본 jQuery 객체가됩니다. self 당신이 이야기 할 것으로 예상되는 포인터로 돌아갑니다.
+0

은 내가 제안 된 변경 사항을하지만 난] 경고 [개체 개체에 [개체 개체를받을 ] 함수 barcodeTotal() { \t \t \t \t var self = this; \t \t \t \t var barcodes = []; \t \t \t \t $ ("입력 : 선택"). 각 (기능 (전, 항목) { \t \t \t \t \t barcodes.push ({ \t \t \t \t \t \t 바코드 : $ (자기) .attr ("바코드"), \t \t \t \t \t \t 금액 : $ (자기) .attr ("값"), \t \t \t \t \t \t 이전 : $ (자체) .attr ("이전") \t \t \t \t \t}); \t \t \t \t \t // barcode [i] = $ (this) .attr ("barcode"); \t \t \t \t \t // barcodeamount [i] = $ (this) .attr ("value"); \t \t \t \t \t // barcodeprevious [i] = $ (this) .attr ("previous"); \t \t \t}); 알림 (바코드); \t \t \t} – kosbou

0

이)} 당신에게

var arrBarCodeDet = new Array(); 
var objCheckedElem = $("input:checked"); 
$.map(objCheckedElem,function(elem,index){ 
    /** 
     Result As Object 
     arrBarCodeDet[index] = [{'barcode':$(elem).attr("barcode")},{'barcodevalue':$(elem).attr("barcodevalue")}]; 
    */ 
    //Result As Arrray 
    arrBarCodeDet[index] = new Array(); 
    arrBarCodeDet[index]['barcode'] = $(elem).attr("barcode"); 
    arrBarCodeDet[index]['barcodevalue'] = $(elem).attr("barcodevalue"); 

}) 

도움이 될 것입니다; 0 [어레이 [0] , 배열 "12233" barcodevalue : "12" 길이 : 0 프로토

<input type="checkbox" name="check1" value="1" checked="checked" barcode="12233" barcodevalue="12" />Check Box 1 <br /> 
<input type="checkbox" name="check2" value="1" checked="checked" barcode="45451" barcodevalue="24" />Check Box 2 <br /> 
<input type="checkbox" name="check3" value="1" checked="checked" barcode="34343" barcodevalue="36" />Check Box 3 <br /> 
<input type="checkbox" name="check4" value="1" barcode="32333" value="48" />Check Box <br /> 

결과 arrBarCodeDet [ 어레이 [0] 바코드 것인가 ] 바코드 "45,451" barcodevalue "24" 길이 : 0 프로토 : 어레이 [0] , 배열 [ 0] 바코드 : "34343" barcodevalue : "36" 길이 : 0 프로토 : 배열 [0] ]

관련 문제