我正在尝试使用以下方法创建一个数组,但到目前为止,我仅设法一个一个地获取数据。我真正希望实现的是存储一个包含所有数据的数组,以便可以使用PHP将其存储在数据库中。
到目前为止,我的问题是我似乎无法用JS创建一个简单的数组,当我尝试使用“ push”时,它将返回空数组。
有人可以帮助我吗?
Sortable.create(simpleList, {
onUpdate: function( /**Event*/ evt) {
saveUnitsPositions();
},
});
function saveUnitsPositions() {
const GETPOSITION = document.querySelectorAll("#simpleList li");
var positions = [];
for (var i = 0; i < GETPOSITION.length; i++) {
positions = GETPOSITION[i].getAttribute('data-value');
console.log(positions);
}
}
<script src="http://SortableJS.github.io/Sortable/Sortable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<ul id="simpleList" class="list-group">
<li class="list-group-item course-unit" data-value="26321">
Etude #1
</li>
<li class="list-group-item course-unit" data-value="26322">
Etude #2
</li>
<li class="list-group-item course-unit disabled" data-value="Backing Tracks">
Backing Tracks
</li>
<li class="list-group-item course-unit" data-value="26326">
Backing track Fusion (118 bpm)
</li>
<li class="list-group-item course-unit" data-value="26327">
Backing track Samba (90 bpm)
</li>
<li class="list-group-item course-unit disabled" data-value="Apostilas">
Apostilas
</li>
<li class="list-group-item course-unit" data-value="26615" style="transform: translateZ(0px);">
Etudes
</li>
<li class="list-group-item course-unit" data-value="26332">
Tríades Mutantes – Apostila Completa
</li>
</ul>
最佳答案
如果尝试不使用Sortable
,则可以使用push方法。也许问题出在Sortable
上。并且使用push
方法是正确的,它应该可以工作。
编辑:我再次尝试,并且它与Sortable,当您更新某个位置时,它填充数组。
Sortable.create(simpleList, {
onUpdate: function( /**Event*/ evt) {
saveUnitsPositions();
},
});
function saveUnitsPositions() {
const GETPOSITION = document.querySelectorAll("#simpleList li");
var positions = [];
for (var i = 0; i < GETPOSITION.length; i++) {
positions.push(GETPOSITION[i].getAttribute('data-value')); // use push here
console.log(positions);
}
}
<DOCTYPE html>
<html>
<head>
<script src="http://SortableJS.github.io/Sortable/Sortable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<ul id="simpleList" class="list-group">
<li class="list-group-item course-unit" data-value="26321">
Etude #1
</li>
<li class="list-group-item course-unit" data-value="26322">
Etude #2
</li>
<li class="list-group-item course-unit disabled" data-value="Backing Tracks">
Backing Tracks
</li>
<li class="list-group-item course-unit" data-value="26326">
Backing track Fusion (118 bpm)
</li>
<li class="list-group-item course-unit" data-value="26327">
Backing track Samba (90 bpm)
</li>
<li class="list-group-item course-unit disabled" data-value="Apostilas">
Apostilas
</li>
<li class="list-group-item course-unit" data-value="26615" style="transform: translateZ(0px);">
Etudes
</li>
<li class="list-group-item course-unit" data-value="26332">
Tríades Mutantes – Apostila Completa
</li>
</ul>
</body>
</html>
关于javascript - 用JavaScript创建数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58810892/