需要在输入中保存元素的索引。 在启动时,我会获得所有字段的索引,但是拖动时字段不会改变。我使用这个库(拖放StartableJs)
<ul class="loop" id="block">
<li class="list-group-item">
<span class="my-handle" id="my-handle" aria-hidden="true"></span>
<input id="index_li" output="bootstrap" placeholder="-">
</li>
<li class="list-group-item">
<span class="my-handle" id="my-handle" aria-hidden="true"></span>
<input id="index_li" output="bootstrap" placeholder="-">
</li>
<li class="list-group-item">
<span class="my-handle" id="my-handle" aria-hidden="true"></span>
<input id="index_li" output="bootstrap" placeholder="-">
</li>
</ul>
此js库SortableJs
var options = {
group: 'Block',
animation: 100,
onEnd: function (/**Event*/evt) {
evt.oldIndex; // element's old index within parent
evt.newIndex; // element's new index within parent
evt.oldDraggableIndex;
evt.newDraggableIndex;
}
};
events = [
'onChoose',
'onStart',
'onEnd',
'onAdd',
'onUpdate',
'onSort',
'onRemove',
'onChange',
'onUnchoose'
].forEach(function (name) {
options[name] = function (evt) {
console.log({
//'event': name,
//'this': this,
//'item': evt.item,
//'from': evt.from,
//'to': evt.to,
//'oldIndex': evt.oldIndex,
'newIndex': evt.newIndex,
//'newDraggableIndex': evt.newDraggableIndex,
//'index': $(this).index()
});
var newindex = evt.newIndex;
var oldindex = evt.oldIndex;
window.onload = function(){
var input_ix = document.getElementsByTagName('input')[$(this).index()];
$( input_ix ).val($(this).index());
};
};
});
Sortable.create(block, options);
需要在li> input的每个输入框中保存newIndex
//$( input_ix ).val(newindex);
//if i do so, I get undefind
如果我获得元素索引,则更新值存在问题