Answer the question
In order to leave comments, you need to log in
How to create multidimensional array using JS loop?
There is a table (Example):
Cell phone fLY BL6201 | X | ||
iPhone 5c | X |
<tr id="tr">
<td width="82">Сотовый телефон fLY BL6201<b><input type="hidden" id="eq28" class="name_eq" value="28"></b></td>
<td width="140"><i><input type="text" name="sn28" class="sn" value="" placeholder="Серийный номер"></i></td>
<td style="width:100px;" align="center"><input type="checkbox" name="garant28"></td><td width="50"><span class="remove_item" style="cursor: pointer; font-weight: bold;color:#ff0000">Х</span></td>
</tr>
<tr id="tr">
<td width="82">iPhone 5c<b><input type="hidden" id="eq5" class="name_eq" value="5"></b></td>
<td width="140"><i><input type="text" name="sn5" class="sn" value="" placeholder="Серийный номер"></i></td>
<td style="width:100px;" align="center"><input type="checkbox" name="garant5"></td><td width="50"><span class="remove_item" style="cursor: pointer; font-weight: bold;color:#ff0000">Х</span></td>
</tr>
var array_eq=[]; //создаю пустой массив
for (v=0;v<document.getElementsByClassName("name_eq").length;v++){ //перебираю все элементы с классов name_eq
array_eq[[v]]=document.getElementsByClassName("name_eq")[v].value; //записываю в массив
}
var lenght_ar=parseInt(array_eq.length); // перевожу в число и получаю кол-во элементов
for (var q = 0; q < lenght_ar; q++) { //дальше все сложно(((
var num_arr=array_eq[q];
var name_ar='[name = sn'+num_arr+']';
array_eq[[[v]]]=$(name_ar).val(); // тут уже только чего не пробовал
alert($(name_ar).val());
}
console.log(array_eq);
Answer the question
In order to leave comments, you need to log in
Try like this:
var array_eq = [],
nameLength = document.getElementsByClassName("name_eq").length;
for (var i = 0; i < nameLength; i++) {
var value = document.getElementsByClassName("name_eq")[i].value;
array_eq.push([value]);
}
for (var i = 0; i < array_eq.length; i++) {
var num_arr = array_eq[i][0];
var name_ar = '[name = sn' + num_arr + ']';
array_eq[i].push($(name_ar).val());
}
console.log(array_eq);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question