2012-02-11 101 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') }) 

,或者什麼可能會更好,因爲像戴爾森建議的物體

var results = checkedInputs.map(function() { 
    return { 
    barcode: $(this).attr('barcode'), 
    amount: $(this).attr('amount'), 
    previous: $(this).attr('previous') 
    } 
}) 

在這種情況下,你想thisfunction通話, 內,因爲它是指對象服用,你與$('input:checked')呼叫匹配。 如果您將var self = this作爲twilson建議存儲,您將不會收到輸入框的實際值,但是沒有值,因爲調用上下文的this很可能根本不是HTMLElement

1

你會b最好用一組對象來服務,而不僅僅是一個數組。

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") 
    }); 
}); 

你會結束了一個數組是這樣的:

[ 
    { barcode: "5049383", amount: "4", previous: "17263742" }, 
    { barcode: "5049389", amount: "1", previous: "1726376" } 
] 
  1. var關鍵字的變量之前否則他們將是全球性的函數中。
  2. var self = this;在調用附加函數時通常是很好的做法,因爲this的範圍可能會根據您的執行點而改變很多,通常會成爲主要的jQuery對象。 self給你指針回到你期望與之交談的地方。
+0

我使所建議的修改,但我收到警報[對象的對象],[對象的對象] function barcodeTotal(){ \t \t \t \t var self = this; \t \t \t \t var barcodes = []; \t \t \t \t $( 「輸入:勾選」)。每個(函數(I,項目){ \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前一個:$(self).attr(「previous」) \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 2012-02-11 08:02:00

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"); 

}) 

});

<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] 條碼: 「12233」 barcodevalue: 「12」 長度:0 :數組[0] , 數組[0 ] 條碼: 「45451」 barcodevalue: 「24」 長度:0 :數組[0] , 數組[ 0] 條碼: 「34343」 barcodevalue: 「36」 長度:0 :數組[0] ]