Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从列表onclick javascript填充数组_Javascript_Jquery_Html_Ajax_Arrays - Fatal编程技术网

从列表onclick javascript填充数组

从列表onclick javascript填充数组,javascript,jquery,html,ajax,arrays,Javascript,Jquery,Html,Ajax,Arrays,我正在用JS编写代码,我不知道当点击按钮时如何填充数组。我们有这个代码,它使用一个列表ul,其中的项目li可以用鼠标移动。如何使用onclick将数组的第一个位置和最后一个位置填充为2个数据 <!doctype html> <html lang="en"> <head> <script src="http://code.jquery.com/jquery-1.9.1.js"></script>

我正在用JS编写代码,我不知道当点击按钮时如何填充数组。我们有这个代码,它使用一个列表ul,其中的项目li可以用鼠标移动。如何使用onclick将数组的第一个位置和最后一个位置填充为2个数据

    <!doctype html>
    <html lang="en">
    <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>    
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>   
    <script src="https://raw.github.com/furf/jquery-ui-touch-punch/master/jquery.ui.touch-punch.min.js"></script> 
    <script>
      $(function() {
        $( ".documents" ).sortable();
        $( ".documents" ).disableSelection();
      });
    </script>
  <meta charset="utf-8">
  <title>toArray demo</title>
  <style>
  span {
    color: red;
  }
  </style>
</head>
<body>

Reversed - <span></span>

<ul id="opciones" class="documents">
<li>uno</li>
<li>dos</li>
<li>tres</li>
</ul>

<script>
function disp( li ) {
  var a = [];
  for ( var i = 0; i < li.length; i++ ) {
    a.push( li[ i ].innerHTML );
  }
  $( "span" ).text( a.join( " " ) );
}
disp( $( "li" ).toArray() );
</script>
 <input type="button" value="actualizar_array" onclick="disp('#opciones')" />
</body>
</html>
在函数disp中,您将参数li视为一个数组,但将其作为字符串发送:disp'opciones'。我想你应该试试这个:

function disp( ul ) {
    var a = [];
    var mainUL = document.getElementByID( ul );
    var li = mainUl.childNodes;
    for ( var i = 0; i < li.length; i++ ) {
        a.push( li[ i ].innerHTML );
    }
    $( "span" ).text( a.join( " " ) );
}

并使用dispopciones而不是disp'opciones来调用它。

您可以使用这样的函数。你要找的是。每种方法

使用可排序插件,您可以从“sortupdate”事件调用updateOrder:

$('#opciones').sortable().bind('sortupdate', function() {
    updateOrder();
});

我做了一把小小提琴给你演示

谢谢,一切都好了,我已经进入了这个位置,只更改了项目X的类名,并在代码中添加了一行,谢谢。行:orderArray.push$li[i].className.slice5;再次感谢您抽出时间。
$('#opciones').sortable().bind('sortupdate', function() {
    updateOrder();
});