问题
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