上下移动选定的元素并将更改的顺序保存在数组中,以便保存订单

时间:2017-08-31 06:52:27

标签: javascript jquery html sharepoint

我的列表框包含来自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;}

1 个答案:

答案 0 :(得分:0)

您是否考虑过创建全局变量?当您保留在页面上时,这些值将被保存。如果您需要在本地或会话中存储它们,可以考虑使用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>