我使用jQuery对<LI></LI>
标签之间的项目列表进行排序,然后使用Ajax帖子来验证顺序并使用返回的内容“更新”页面。jQuery在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>
你为什么要更换全''元素?这当然会摆脱你的完整的Javascript代码...你应该重新考虑你的语义 – devnull69
只是一个想法,但是你在ajax url中第一次被调用之后声明$ id。你也应该只重新加载列表,而不是整个HTML页面。我不太清楚究竟是什么问题? –
一个好的提示是检查您的JavaScript控制台,大多数浏览器有JavaScript错误(http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-浏览器/ 8555) – rednaw