EmW*_*mWe 3 ajax jquery serialization jquery-ui
我有一个列表,其中的项目应由用户使用jQuery UI Sortable进行排序.在用户选择了他的最终订单后,他必须单击"就绪"按钮.单击按钮后,应使用serialize和Ajax将订单发送到saveoder.php.
我尝试用click事件包围ajax调用,但是根据用户的可排序操作的数量,将会有几个POST请求完成.我只需要一个ajax post请求.
$(function() {
$( "#sortable" ).sortable({
update: function(event, ui) {
var order = $(this).sortable('serialize');
$(document).on("click", "button" , function() { //that doesn't work
$.ajax({
data: order,
type: 'POST',
url: 'saverank.php'
});
});
}
}).disableSelection();
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<ul id="sortable">
<li id="id_1">Item 1</li>
<li id="id_2">Item 2</li>
<li id="id_3">Item 3</li>
</ul>
<button>Ready</button>Run Code Online (Sandbox Code Playgroud)
有一种内置的方法可以做到这一点.请参阅serialize或toArray
演示:http://jsfiddle.net/lotusgodkk/GCu2D/539/
JS:
$(function () {
$("#sortable").sortable({
update: function (event, ui) {
var order = $(this).sortable('serialize');
$(document).on("click", "button", function () { //that doesn't work
$.ajax({
data: order,
type: 'POST',
url: 'saverank.php'
});
});
}
}).disableSelection();
$('button').on('click', function () {
var r = $("#sortable").sortable("toArray");
var a = $("#sortable").sortable("serialize", {
attribute: "id"
});
console.log(r, a);
});
});
Run Code Online (Sandbox Code Playgroud)