我正在使用 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
}
但它不起作用......
我不知道该怎么做。有人可以帮忙吗?
我剥离了代码的主要部分,只保留必要的部分(文件名: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>