在Ajax帖子之后jQuery不起作用

时间:2012-10-19 10:34:24

标签: jquery ajax

我正在使用jQuery对<LI></LI>标签之间的条目列表进行排序,然后使用Ajax帖子对订单进行排序,并使用返回的内容“更新”页面。

$.ajax({url: "./test.php?id=<?php echo $id; ?>&action=modify",
    contenttype: "application/x-www-form-urlencoded;charset=utf-8",
    data: {myJson:  data},
    type: 'post',
    success: function(data) {
        $('html').html(data);
        OnloadFunction ();
        }
    });

然后,我失去了对列表进行排序的能力(我不确定是否清楚......)。我尝试在$(document).ready内移动OnloadFunction ()的内容,并在处理修改的块中用<script>OnloadFunction ();</script>调用它:

$action= $_GET['action'];
if ($action == "modify") {
// Code here
}

但它不起作用......

我无法弄清楚如何做到这一点。有人可以帮忙吗?

我删除了代码的主要部分,只保留必要的(filename:test.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="jquery-ui-1.9.0.custom.min.js"></script>
    <script>
    $(document).ready(function(){ 
        //alert("I am ready");
        OnloadFunction ();
        }); 

    function OnloadFunction () {
            $(function() {
            $("#SortColumn ul").sortable({ 
                opacity: 0.6, cursor: 'move',
                update: function() {}                                 
                });
            });
            //alert('OnloadFunction ends');
        }

    function valider(){
        var SortedId = new Array(); 
        SortIdNb = 0;
        $('#SortColumn ul li').each(function() {
            SortedId.push(this.id);
        });
            var data = {
            /* Real code contains an array with the <li> id */
            CheckedId: "CheckedId",
            SortedId: SortedId,
            };
        data = JSON.stringify(data);
        $.ajax({url: "./test.php?id=<?php echo $id; ?>&action=modify",
            contenttype: "application/x-www-form-urlencoded;charset=utf-8",
            data: {myJson:  data},
            type: 'post',
            success: function(data) {
                //alert(data);
                $('html').html(data);
                OnloadFunction ();
                }
            });
        }
    </script>
</head>
<body>
<?

$action= $_GET['action'];
$id = $_GET['id'];
if ($id == 0) {$id=1;}
$id += 1;
if ($action == "modify") {
    echo "action: modify<br>";
    echo "id (àvèc aççént$): ".$id."<br>"; // "(àvèc aççént$)" to check characters because character set is incorrect after the ajax post
    $data = json_decode($_POST['myJson'], true);
    // PHP code here to treat the new list send via the post and update the database
    print_r($data);
    }
?>
<!-- PHP code here to get the following list from the database -->
<div id="SortColumn">
    <ul>
        <li id="recordsArray_1">recordsArray_1</li>
        <li id="recordsArray_2">recordsArray_2</li>
        <li id="recordsArray_3">recordsArray_3</li>
        <li id="recordsArray_4">recordsArray_4</li>
        <li id="recordsArray_5">recordsArray_5</li>
    </ul>
</div>
<input type="button" value="Modifier" onclick="valider();">

</body>
</html>

1 个答案:

答案 0 :(得分:1)

将您的代码更改为此

$(document).ready(function(){ 
    //alert("I am ready");
    OnloadFunction ();
    }); 

function OnloadFunction () {

        $("#SortColumn ul").sortable({ 
            opacity: 0.6, cursor: 'move',
            update: function() {}                                 
            });

        //alert('OnloadFunction ends');
    }