php+jquery拖动并更改排序的完整代码
勾选后拖动可以实时保存更新后的数据
XML/HTML Code
- <script type="text/javascript">
- jQuery(document).ready(function() {
- //保存常用选择器
- var list = jQuery("#list"); //ul
- var show = jQuery("#show"); //输出提示
- var orderlist = jQuery("#orderlist"); //原顺序
- var check = jQuery("#check"); //是否更新到数据库
- //保存原来的排列顺序
- var order = [];
- list.children("li").each(function() {
- order.push(this.title); //原排列顺序保存在title,得到后更改title
- jQuery(this).attr("title", "你可以拖动进行排序");
- });
- orderlist.val(order.join(','));
- //ajax更新
- var Update = function(itemid, itemorder) {
- jQuery.ajax({
- type: "post",
- url: "update.php",
- data: { id: itemid, order: orderlist.val() }, //id:新的排列对应的ID,order:原排列顺序
- beforeSend: function() {
- show.html("正在更新");
- },
- success: function(req) {
- if (req == "100") {
- show.html("更新成功");
- }
- else if (req == "001") {
- show.html("失败,请稍后再试");
- }
- else {
- show.html("参数不全");
- }
- }
- });
- };
- //调用ajax更新方法
- var Submit = function(update) {
- var order = [];
- list.children("li").each(function() {
- order.push(this.id);
- });
- var itemid = order.join(',');
- //如果单选框选中,则更新表中排列顺序
- if (update) {
- Update(itemid);
- }
- else {
- show.html("");
- }
- };
- //执行排列操作
- list.sortable({
- opacity: 0.7,
- update: function() {
- Submit(check.attr("checked"));
- }
- });
- });
- </script>
PHP Code
- <form id="form1" runat="server">
- <span id="show"></span>
- <div> 拖动时同时更新数据库数据:
- <input type="checkbox" id="check" />
- </div>
- <div>
- <input type="hidden" id="orderlist" />
- <?php
- require "../../conn.php";
- $query = mysql_query("SELECT * FROM `orderlist` ORDER BY `sortid` ASC");
- ?>
- <ul id="list">
- <?php
- while($row=mysql_fetch_array($query))
- {?>
- <li id="<?php echo $row['id']?>" title="<?php echo $row['sortid']?>"><img src="../<?php echo $row['pic']?>" width="200"/></li>
- <?php }?>
- </ul>
- </div>
- </form>
原文地址:http://www.freejs.net/article_jquerytupiantexiao_590.html