jQuery+Ajax实现排序效果

来源:https://www.sucaihuo.com/js/389.html 2015-08-21 08:15浏览(4867) 收藏

在开发项目中,我们会经常碰到文章排序功能,也就是把优秀的文章推荐到前面。
jQuery+Ajax实现排序效果
分类:PHP > Ajax 难易:初级
下载资源 下载积分: 30 积分

PHP

<table width="100%" cellspacing="0" cellpadding="0" border="0" class="table_parameters">
    <tbody>
        <tr class="tr_head">
            <td>用户名</td>
            <td>内容</td>
            <td width="120px">排序</td>
        </tr>
        <?php
        $sql = "SELECT name,content,id,ord FROM wishing_wall ORDER BY ord ASC limit 0,10 ";
        $query = mysql_query($sql);
        while ($row = mysql_fetch_array($query)) {
            ?>

            <tr>
                <td align="center"><?php echo $row['name']; ?></td>
                <td><?php echo $row['content']; ?></td>
                <td align="center"><a onclick="changeOrd($(this), '<?php echo $row['id']; ?>')"><?php echo $row['ord']; ?></a></td>
            </tr>   
        <?php } ?>
    </tbody>
</table>

JS

function changeOrd(obj, id) {
    var val = obj.text();
    var c = obj.parent("td");
    obj.parent("td").html("<input type='text' style='width:50px;' onFocus=this.select()  onblur=changeOrdConfirm($(this)," + id + ") value='" + val + "' />");
    c.children("input").focus();
}
function changeOrdConfirm(obj, id) {
    var ord = obj.val();
    $.post("ajax.php", {
        id: id,
        ord: ord
    },
    function(data) {
        obj.parent("td").html("<a onclick=changeOrd($(this)," + id + ")>" + obj.val() + "</a>");
    })
}

ajax.php

include_once("connect.php");

$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$ord = isset($_POST['ord']) ? intval($_POST['ord']) : 0;
if ($id > 0) {
    $sql = "UPDATE `wishing_wall` SET `ord` = '".$ord."' WHERE `id` = '" . $id . "';";
     mysql_query($sql);
}
标签: 排序
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2