我有一些带输入的行。我想为每行创建一个包含输入值的数组。所以,如果我有3行,并且输入值为'输入1'是1,输入值2'是2和输入3'的值是3,数组看起来像这样:
[
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]
]
这是我的HTML代码:
<div class="container">
<div class="row">
<input id="input1">
<input id="input2">
<input id="input3">
</div>
<div class="row">
<input id="input1">
<input id="input2">
<input id="input3">
</div>
<div class="row">
<input id="input1">
<input id="input2">
<input id="input3">
</div>
</div>
我怎么能在jQuery中做到这一点?
答案 0 :(得分:0)
据我了解你的问题,我会做这样的事情:
var obj = {};
$('.row').each(function(ind1) {
obj[ind1] = {};
$(this).children().each(function (ind2) {
obj[ind1][ind2] = $(this).val();
});
});
答案 1 :(得分:0)
HTML:
<div class="container">
<div class="row">
<input class="input1">
<input class="input2">
<input class="input3">
</div>
<div class="row">
<input class="input1">
<input class="input2">
<input class="input3">
</div>
<div class="row">
<input class="input1">
<input class="input2">
<input class="input3">
</div>
</div>
Javascript:
var data = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
$('.container .row').each(function(row) {
$(this).children().each(function (column) {
$(this).val(data[row][column]);
});
});
OR
HTML:
<div class="container"></div>
Javascript:
var data = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
for(var i = 0; i < data.length; i++){
var arr = data[i];
var row = $(".container").append("<div class='row'></div>");
for(var j = 0; j < arr.length; j++){
var i_name = 'input-'+i+'-'+j;
row.append('<input id="'+i_name+'" type="text" name="'+i_name+'" value="'+arr[j]+'">');
}
}