2012-02-11 132 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] ]