2011-12-17 77 views
0

我一直在研究过去两天,并没有发现任何东西。如何从加载div加载sql /会话内容到一个div

结构:

的index.php:

<head> 
    <script type="text/javascript" src="JS/jquery-1.6.2.js"></script> 
    <script type="text/javascript" src="function.js"></script> 
    </head> 

    <body> 

    <div> 
    <div><a href="" class="testcat" id="1">Show</a></div> *-->if I click this link data loads into DIV below by function.js without reloading* 
    <div id="producten"></div> *-->testpage.php loads here perfect, 
the code of testpage.php makes by while loop other links. 
Here I want to click on a link to load next data in div id=information 
without reloading the index.php so the data in the first DIV stay visible 
and I dont know how to do that* 
    <div id="information"></div> *-->testpage2.php have to load data from sql in this DIV* 
    </div> 

    </body> 

function.js:

$(document).ready(function() { 
    $(".testcat").click(function() { 
     var testid = $(this).attr("id"); 
     var datastring = 'id='+ testid ; 
     $.ajax({ 
      type: "POST", 
      url: "testpage.php", 
      data: datastring, 
      cache: false, 
      success: function(res) { 
      $('#producten').html("<div class='loading'><img src='IMG/loading.gif' /></div>") 
       .hide() 
       .fadeIn(2000, function() { 
       $('#producten').html(res); 
      }) 
      } 
     }); 
     return false; 
    }); 

}); 

testpage.php和testpage2.php是用于SQL数据PDO代码。

+0

这是一个有点很难理解你想要什么。你能否显示'testpage.php'和'testpage2.php'的完整代码,并指定在哪个第二个div中加载信息? – 2011-12-17 15:54:06

+0

如果我理解正确,那么您正在关注一个AJAX解决方案。 – 2011-12-17 18:07:22

回答

0

你想与on附上点击处理,使动态添加的内容仍然向他们提供同样的AJAX处理程序:

添加任何信息需要从下一个区分点击,即

<a href='...' data-resultdiv='production' 

然后,清理你的处理了一下:我想,你希望Ajax请求去链接的href,那你想显示“加载” 立即(而不是等待请求完成)。

最后,要取消主播浏览到href引用页面的默认行为,可以返回false;

$(document).on("click", "a", function() { 
    var href = $(this).attr("href"); 
    var successDiv = $(this).data("resultdiv"); 
    $('#' + successDiv).html("<div class='loading'><img src='IMG/loading.gif' /></div>"); 
    $.ajax({ 
     type: "POST", 
     url: href, 
     data: datastring, 
     cache: false, 
     success: function(res) { 
      $('#' + successDiv).hide().html(res).fadeIn(2000); 
     }  
    } 
    return false; 
}); 

当然如果你只希望这对某些锚运行,你可以把一个选择你的电话就

$(document).on("click", "a.someClass", function() {