私は配列をループしていますが、配列の長さのインスタンスが複数取得されています。 jQueryのでこれを回避するためのいずれかのインjQueryこの `closure`を修正する正しい方法
var array = [{
name: 'name1',
value: [1, 2, 3, 4]
}, {
name: 'name2',
value: ['a', 'b', 'c', 'd']
}, {
name: 'name3',
value: ['a', 'b', 'c', 'd']
}];
var makeDropDown = function() {
var newhtml = function(value) {
var name = $('<td/>', {
text: value.name
});
var build = $('<tr/>').append(name).append($('<td/>'));
$("tbody").append(build);
}
if (!array.length)
return;
$.each(array, function(index, value) {
newhtml(value); //my try to avoid clousure not works!
})
}
makeDropDown();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<theader>
<th>S.No</th>
<th>Name</th>
</theader>
<tbody></tbody>
<tfooter></tfooter>
</table>
ビルド方法はありますか?
問題 –
ことが何であるかでなければなりません?どうぞお分かりですか? – 3gwebtrain
引数を 'newhtml(value);'に渡します。したがって、それは 'newhtml(index);となります。 –