1

我有一个填充了 AJAXonchange请求结果的表。一切正常,但我还有一个包含在 window.load 上的 JS 脚本,它基于mouseovermouseoutonclick等更改表。但是,由于在 AJAX 请求之前不会填充表,因此表 JS 功能不起作用。有没有办法用 AJAX 请求填充表格,而不是做 window.load,在 AJAX 请求完成期间或之后加载 JS 脚本?下面是我的代码:

<link href="<?php echo $this->config->item('base_url'); ?>/css/table_design.css" 
      rel="stylesheet" type="text/css" media="screen" />
    
// This is the script that is included, and inside the script
//the main function is started by window.load
<script type="text/javascript" 
    src="<?php echo $this->config->item('base_url'); ?>/js/table_design.js"></script>
    
<br />
                
<script type="text/javascript">
    function showUser(str) {
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()   {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)   {
                document.getElementById("tars").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","jqueryDB/catSelector?cat="+str,true);
        xmlhttp.send();
}
</script>

<span id="tars"></span>

这是我通过 AJAX 请求的 PHP 页面:

public function catSelector()
{
    $catID = $_GET['cat'];  
    $query = $this->db->query('SELECT * FROM upload WHERE category = '.$catID);
    $num_rows = $query->num_rows();
    $val = $query->result_array();   
            
    echo '<table cellspacing="0" cellpadding="0">
        <tr>
        <th width="50%">File Name:</th>
        <th width="30%">File Size:</th>
        <th>Download Link:</th>
    </tr>';
        
    $c = true;
    foreach($val as $files){
        
         if ($files['size'] >= 1000000000) {
             $files['size'] = round( ($files['size'] / 1000000000) , 2).' GB';
        }
        
        if ($files['size'] >= 1000000) {
            $files['size'] = round( ($files['size'] / 1000000) , 2).' MB';
        }else{
            $files['size'] = round( ($files['size'] / 1000) , 2).' KB';
        }
                
        echo '<tr'.(($c = !$c)?' class="odd"':'').">
            <td>$files[name]</td>
            <td>$files[size]</td>
            <td><a href='$files[location]' >Download</a></td>
        </tr>";
    }
        
    echo '</table>';
}
4

2 回答 2

1

我从未使用过XMLHttpRequest,但阅读后我认为您可以这样做:

function showUser(str)  {
    ...
    xmlhttp.open("GET","jqueryDB/catSelector?cat=" + str, true);
    xmlhttp.onload = function() { //add click events to table elements (same function you calling now with `window.load` };
    xmlhttp.send();
}
于 2012-08-19T18:09:24.977 回答
0

您能否发送您在窗口加载时执行的 js 脚本?还尝试调用脚本,如下所示:-

xmlhttp.onreadystatechange=function()  
                      if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        document.getElementById("tars").innerHTML=xmlhttp.responseText;
// call the js script here example test();
于 2012-08-19T18:11:52.247 回答