How to pass sortable order to hidden field in jQuery?

人盡茶涼 提交于 2019-12-11 23:45:26

问题


I'm trying to pass the values of a sortable list to a hidden field using the code below, but no luck:

<script>
   $(document).ready(function(){

  $(function() {
    $( "#sortable1, #sortable2" ).sortable({
      connectWith: ".connectedSortable"
    }).disableSelection();
  });

  var idsInOrder = [];

  $("#form_quest").submit(function(){
    $("ul#sortable2 li.card").each(function() { idsInOrder.push($(this).attr('title')); });

    $("#sort_order").val(idsInOrder);
  });  
});
</script>

html hidden field:

<input type="hidden" name="sort_order" id="sort_order" value=""/>

What am I doing wrong?

Thanks in advance.


回答1:


Instead of doing it on the form submit, do it when the order has been updated

Example: http://jsfiddle.net/4zxLkfv6/5/

var idsInOrder = [];
$('.sortable').sortable({
  update: function( event, ui ) {
      idsInOrder = [];
      $('.sortable li').each(function() {
          idsInOrder.push($(this).attr('id'));
      });
      $('#sort_order').val(idsInOrder);
  }
});



回答2:


To obtain title1,title2,title3 in your field:

$("#sort_order").val(idsInOrder.join(','));


来源:https://stackoverflow.com/questions/27922868/how-to-pass-sortable-order-to-hidden-field-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!