[英]jQuery doesn't work after an Ajax post
我正在使用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>
将您的代码更改为此
$(document).ready(function(){
//alert("I am ready");
OnloadFunction ();
});
function OnloadFunction () {
$("#SortColumn ul").sortable({
opacity: 0.6, cursor: 'move',
update: function() {}
});
//alert('OnloadFunction ends');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.