上下移动选定的元素,并将更改后的顺序保存在阵列中,以便保存该顺序



我的列表框包含javascript对象数组中的值(AllLinks(。

[{"AllLinks":"LinkOne","LinkURL":"http://www.Link1.com","Order":"0"},{"AllLinks":"Link3","LinkURL":"http://www.Link3.com","Order":"2"},{"AllLinks":"Link4","LinkURL":"http://www.Link4.com","Order":"3"},{"AllLinks":"Link5","LinkURL":"http://www.Link5.com","Order":"4"}]

我想上下移动选定的元素,还想将更改后的顺序保存在数组中,以便将其保存回sharepoint列表中。下面的代码完美地改变了顺序,但我如何保存和保留值

function MoveUp(lst){
if(lst.selectedIndex == -1)
alert('Please select an Item to move up.');
else
{
if(lst.selectedIndex == 0)
{
alert('First element cannot be moved up');
return false
}
else
{
var tempValue = lst.options[lst.selectedIndex].value;
var tempIndex = lst.selectedIndex-1;
lst.options[lst.selectedIndex].value = lst.options[lst.selectedIndex-1].value;
lst.options[lst.selectedIndex-1].value = tempValue;
var tempText = lst.options[lst.selectedIndex].text;
lst.options[lst.selectedIndex].text = lst.options[lst.selectedIndex-1].text;
lst.options[lst.selectedIndex-1].text = tempText;
lst.selectedIndex = tempIndex;
}
}
return false;}

您想过创建全局变量吗?当您停留在页面上时,这些值将被保存。如果您需要在本地或会话中存储它们,可以考虑使用localStorage或sessionStorage

<!DOCTYPE html>
<html>
<head>            
<title>Ejemplo sessionStorage</title>   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<script>
$(document).ready(function(){    
$('#boton-guardar').click(function(){        

var nom = document.getElementById("nombretxt").value;
var apel = document.getElementById("apellidotxt").value;

sessionStorage.setItem("Nombre", nom);
sessionStorage.setItem("Apellido", apel);

document.getElementById("nombretxt").value = "";
document.getElementById("apellidotxt").value = "";
});   
});
$(document).ready(function(){    
$('#boton-cargar').click(function(){                       

var nombre = sessionStorage.getItem("Nombre");
var apellido = sessionStorage.getItem("Apellido");

document.getElementById("nombre").innerHTML = nombre;
document.getElementById("apellido").innerHTML = apellido; 
});   
});
</script>
</head>

<center><h1>Ejemplo - sessionStorage</h1>
<input type="text" placeholder="Nombre" id="nombretxt"> <br>  <br>   
<input type="text" placeholder="Apellido" id="apellidotxt"><br>  <br>
<button id="boton-guardar">Guardar</button><br>

<hr />
Nombre almacenado:
<label type="text" id="nombre"></label><br>                          
Apellido almacenado:
<label "text" id="apellido"></label><br>
<button id="boton-cargar">
Cargar elementos
</button>
</center>
<hr />
</body> 
</html>

最新更新