1

我一直在使用 sortable() 功能,并且我已经让它在静态数据库结果上运行良好,但是我注意到我无法让它在 AJAX 生成的数据库结果上工作。我觉得这与我如何在 $(document).ready(function() 中调用 sortable() 函数有关,但我不确定 100%。我已经搜索了这个论坛和其他论坛,看看是否有其他人有这个问题,但我还没有发现任何类似的东西,所以这是我描述我的问题的尝试:

我有一个名为“db_objects”的 MySQL 数据库,其中包含 1 个表:

CREATE TABLE IF NOT EXISTS `tbl_objects` (
  `obj_id` int(11) NOT NULL AUTO_INCREMENT,
  `obj_name` varchar(50) DEFAULT NULL,
  `obj_type` int(11) DEFAULT NULL,
  PRIMARY KEY (`obj_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

INSERT INTO `tbl_objects` (`obj_id`, `obj_name`, `obj_type`) VALUES
(1, 'Sam', 1),
(2, 'John', 1),
(3, 'Tom', 1),
(4, 'Bob', 1),
(5, 'Fluffy', 2),
(6, 'Paws', 2),
(7, 'Kitty', 2),
(8, 'Tibbles', 2),
(9, 'Mr. Meow', 2);

现在,我有一个 HTML 页面,我想根据一个下拉框查看该表上的结果,该下拉框按 obj_type 列过滤(1 = 人名,2 = 猫名):

<!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><title>SORTABLE EXAMPLE</title>
<link rel='stylesheet' href='http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css'>
<script src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script src='http://code.jquery.com/ui/1.9.2/jquery-ui.js'></script>
<style>
div.div_container {
 background:#0F6;
 max-width:400px;
 padding: 10px 10px 10px 10px;
}
h2.h2_med_green { 
 display: inline;
 font-weight:normal;
 color:#090;
 font-size: 1.4em;
}
ul { 
 list-style: none;
 padding: 0px 0px 0px 0px;
 margin: 0px 0px 0px 0px;
}
li.li_sortable {
 cursor: hand; 
 cursor: pointer;
 border-bottom: 1px dotted #999999;
 background:#DDDDDD;
 height: 31px;
   line-height: 31px;
}
</style>
<script type='text/javascript'>
 $(document).ready(function() {
 //CALL SORTABLE FUNCTIONALITY
 $('#ul_sortable').sortable();
 //CALL AJAX RESULTS
 showAJAXResults(1);
 });
</script>
</head>
<body>
<div class="div_container">
    Click and drag each round to re-order.<br />Object Type:&nbsp;
    <select id="ajax_select_type">
     <option value="1">Human</option>
        <option value="2">Feline</option>
    </select>
    <div id="ajax_results"></div>
</div>
<script>
$("#ajax_select_type").change( function() {
 //CALL AJAX DBR
 showAJAXResults($(this).val());
});
function showAJAXResults(obj_type){

 //HTTP REQUEST CODE
 if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();} // code for IE7+, Firefox, Chrome, Opera, Safari
 else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5    
 xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200){document.getElementById("ajax_results").innerHTML=xmlhttp.responseText;}}

 //ASSEMBLE THE PAGE CALL VALUE
 var page_call_value = "ajax/ajax_page.php?t="+obj_type;

 //CALL THE AJAX PAGE
 xmlhttp.open("GET",page_call_value,true);
 xmlhttp.send();
} 
</script>
</body>
</html>

最后,上面的页面链接到以下通过 AJAX 调用的 PHP 页面:

<?php 
 //DETERMINE INPUT VALUES 
 $object_type=$_GET["t"]; 

 //CONNECT TO THE DATABASE 
 $con = mysql_connect("localhost","<USERNAME>","<PASSWORD>"); 
 mysql_select_db("db_objects", $con); 

 //GET SQL QUERY 
 $q_db_result = "SELECT DISTINCT obj_id, obj_name, obj_type 
 FROM tbl_objects 
 WHERE obj_type = '".$object_type."' 
 ORDER BY obj_name"; 

 //Execute the FULL_RESULT_QUERY QUERY 
 $sql_results = mysql_query($q_db_result) or die(mysql_error()); 

 //TURN THE RESULTS RETRIEVED INTO AN ARRAY 
 $results_array = array(); 
 while($row = mysql_fetch_assoc($sql_results)){ 
 $results_array[$row['obj_id']]['obj_name'] = $row['obj_name']; 
 $results_array[$row['obj_id']]['obj_type'] = $row['obj_type']; 
 } 

 //ASSEMBLE THE RESULTS 
 if(sizeof($results_array)>0){ 

 foreach($results_array as $obj_id => $result_details){ 
 //DECLARE VARIABLES 
 $obj_name=$result_details['obj_name']; 
 $obj_type=$result_details['obj_type']; 

 $return_value .= "<li class='li_sortable' value='".$obj_id."'><h2 class='h2_med_green'>".$obj_name."</h2></li>\n"; 
 } 

 //PUT THE UL TAGS AROUND THE HTML CODE 
 $return_value = "<ul id='ul_sortable'>\n".$return_value."</ul>\n"; 

 //PUT THE RESULTS IN THE INNER CONTAINER DIV 
 $return_value = "<div id='ajax_results_ic' class='ajax_results_ic_2'>".$return_value."</div>"; 

 }else{ 
 $return_value = "NO RESULTS RETURNED"; 
 } 

 //DISPLAY HTML CODE 
 echo $return_value; 

 //CLOSE DB CONNECTION 
 mysql_close($con); 
?>

现在,如果你可以让它工作,你会注意到 sortable() 功能不起作用。正如我之前提到的,我认为这与数据库结果是由 AJAX 即时生成的事实有关,因此尝试在 $(document).ready(function 中声明/调用 sortable() 功能() 不起作用,即

$(document).ready(function() {
 $('#ul_sortable').sortable();
});

我不知道如何解决这个问题 - 有人有什么建议吗?提前致谢!

4

3 回答 3

0

您可以sortable()在附加 reponseText 后调用该函数..以便它获取动态生成的 DOM 元素...

xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200){document.getElementById("ajax_results").innerHTML=xmlhttp.responseText;$('#ul_sortable').sortable();}}  //here
于 2013-01-02T08:13:33.880 回答
0

ul_sortable 来自加载 HTML 时不包含的 ajax_page.php。所以你可以把 $('#ul_sortable').sortable(); 在您的 ajax 请求完成后。

于 2013-01-02T08:26:25.223 回答
0

尝试使用这种方式:

<script>
function showAJAXResults(obj_type){
  //HTTP REQUEST CODE
  if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();} // code for IE7+, Firefox, Chrome, Opera, Safari
 else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5    
 xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200)     {document.getElementById("ajax_results").innerHTML=xmlhttp.responseText;}}

 //ASSEMBLE THE PAGE CALL VALUE
 var page_call_value = "ajax/ajax_page.php?t="+obj_type;

 //CALL THE AJAX PAGE
 xmlhttp.open("GET",page_call_value,true);
 xmlhttp.send();
} 

$(document).ready(function() {     // <--------doc ready handler for the change event
   $("#ajax_select_type").change(function() {
     //CALL AJAX DBR
     showAJAXResults($(this).val());

     $('#ul_sortable').sortable(); // <----- put it here after showAJAXResults() function

   });
});
</script>

不确定,但try putting it after your ajax call在里面的change功能dropdowndoc ready handler

于 2013-01-02T08:32:54.113 回答