-2

我已经搜索 Stack Overflow 一个多小时了,似乎找不到解决这个问题的方法。为了减少此 PHP 页面的加载时间,我尝试将其他页面的内容放入 div 中。

页面查询正确,我可以在 Safari (Inspector) 和 FireBug 中看到输出。

success: function(output_string)不会加载里面的内容。我已经尝试了所有可能的组合。var self = this;尽管我认为这可能是由于jQuery(document).ready(function() {但我使用的是 jQuery 的 2 个版本,但我也尝试过,但没有运气。

这是我的附加代码:

<script type="text/javascript">

jQuery(document).ready(function() {
  jQuery(".text").hide();
  //toggle the component with class msg_body
  jQuery(".ttop2").click(function() {
    jQuery(this).next(".text").slideToggle(500);

    var expanded = "./expand.png";
    var collapsed = "./collapse.png";
    var src = $(this).find(".toggle").attr('src');

<? if($show == 'off') { ?>

    if (src == expanded){
      $(this).find(".toggle").attr('src',collapsed);
      var office = $(this).attr("id");
      var month = "<? echo $month; ?>";
      var year = "<? echo $year; ?>";
      $(this).closest(".loading").html("Loading...");
      alert(office);

      $.ajax({
        url: 'LTData.php',
        type:'POST',
        data: 'office=' + office + '&month=' + month + '&year=' + year,
        dataType: 'json',
        success: function(output_string){
            $(this).find(".loading").html("");
            $(this).find(".text").html(output_string);
        } // End of success function of ajax form
      }); // End of ajax call 

    } else {
      $(this).find(".toggle").attr('src',expanded);
      var office = $(this).attr("id");
      alert(office);
    }

  });
});

<? } else {
//..... Etc.
 ?>

PS:我今天开始 Javascript 编程,所以我希望得到一些批评性的回应。谢谢!

编辑:脚本在头脑中。

编辑 2:附加 HTML/PHP。

    $sqlb="SELECT * FROM league ORDER by total DESC"; 
    $resultb=mysql_query($sqlb);
    $num=mysql_num_rows($resultb);
    while($rowsb=mysql_fetch_array($resultb)){

    if($rowsb[total] == '0') {
        continue;
    }


if($col=='ebdff2'){

$col='efefef';

}else{

$col='ebdff2';

}
?>

<table class="ttop2" id="<? echo $rowsb["office"]; ?>" bgcolor="#<? echo $col;?>" width="100%">
    <tr>
        <td width="50%" align="left"><b style="text-transform: capitalize; color:rgb(98, 188, 70);"><? echo $rowsb[office];?></b></td>
        <td width="20%" align="left"><b style="text-transform: capitalize; color:rgb(98, 188, 70);"><? echo $rowsb[leads];?></b></td>
        <!-- <td width="200px" align="left">Total leads</td>
        <td width="270px" align="left">Leads converted</td>-->
        <td width="20%" align="left"><b style="text-transform: capitalize; color:rgb(98, 188, 70);"><? echo $rowsb[total];?></b></td>
        <td width="10%" align="left"><img style="float: right;" class="toggle" alt="" src="./expand.png" /></td>
    </tr>
</table>

<div class="text"><div class="loading"></div></div>

<? } ?>

<?
}///end of show what...
?>
4

3 回答 3

3

在 ajax 成功函数中,您失去了this参考。您需要在函数之前缓存它:

var $this = $(this); //Here
$.ajax({
    url: 'LTData.php',
    type:'POST',
    data: 'office=' + office + '&month=' + month + '&year=' + year,
    dataType: 'json',
    success: function(output_string){
        $this.find(".loading").html("");
        $this.find(".text").html(output_string);
    } // End of success function of ajax form
}); // End of ajax call 

或者,正如 Kevin B 所说,在上下文选项中传递它:

$.ajax({
    url: 'LTData.php',
    type:'POST',
    data: 'office=' + office + '&month=' + month + '&year=' + year,
    dataType: 'json',
    context : this,
    success: function(output_string){
        $(this).find(".loading").html("");
        $(this).find(".text").html(output_string);
    } // End of success function of ajax form
}); // End of ajax call 
于 2013-08-30T14:54:28.713 回答
0

 jQuery(".ttop2").click(function() {

var parent = $(this);

并将您对 $(this) 的所有调用替换为父级。您在使用 $(this) 时遇到了很多外壳问题,因此请先解决此问题,并让我们知道这是否有帮助。

于 2013-08-30T14:59:48.447 回答
0

下面的代码需要写在成功事件下,其中response是html中返回的数据。

//use filter to find an element is at the root level of response.

  if ($(response).filter('#Display').length > 0) { }

//use find to search an element is not at the root level of response.

  if ($(response).find('#AddNew').length > 0) { }
于 2013-08-30T15:04:30.273 回答