简体   繁体   English

通过ajax加载内容

[英]load content by ajax

i have created this ajax script for loading data. 我已经创建了这个ajax脚本来加载数据。

$("#submit").click(function(e){
    e.preventDefault()  
    var loading = "<img src='/images/loading.gif' alt='Loading...' />";
    var scrolltop=$('#scrollbox').attr('scrollTop');
    var scrollheight=$('#scrollbox').attr('scrollHeight');
    var windowheight=$('#scrollbox').attr('clientHeight');
//  var post = $(this).attr("name") + "=" + $(this).val();
    var form_data = $('#search_form').serialize();// + "&" + post ;

    var scrolloffset=20;
    $('#search').html(loading);
    $.post("dosearch.php",form_data,function(newitems) {
        $('#content').append(newitems);
            });
        if(scrolltop>=(scrollheight-(windowheight+scrolloffset))){
            $.post("dosearch.php",form_data,function(newitems) {
        $('#content').append(newitems);
            });
        }     
        });
});

this script will take form data and serialize them and post them to dosearch.php after that dosearch will post data which we search for Limited by 0,6. 此脚本将采用表格数据并将其序列化,然后将其发布到dosearch.php之后dosearch将发布我们搜索受限于0.6的数据。 when scroller reach to bottom of result's div i want to fetch more content and for that reason i created this function which will take our form data again and number of div in my result div...ill then use this div result just in case to limit $number,6. 当滚动条到达结果div的底部时,我想获取更多内容,因此我创建了此函数,该函数将再次获取表单数据和结果div中的div数量...然后使用此div结果以防万一限制$ number,6。

function scroll_result(form_data){
    var loading = "<img src='/images/loading.gif' alt='Loading...' />";
    var scrolltop=$('#scrollbox').attr('scrollTop');
    var scrollheight=$('#scrollbox').attr('scrollHeight');
    var windowheight=$('#scrollbox').attr('clientHeight');
    var scrolloffset=20;
    var divnumber = $('#content').children().size();
    var form_data = form_data + "&size=" + divnumber;

    if(scrolltop>=(scrollheight-(windowheight+scrolloffset)))
    {
        //fetch new items
        $.post('dosearch.php', form_data, function(newitems){
        $('#content').append(newitems);
        });
    }
    setTimeout('scroll_result(form_data)', 1500);
}

this function dont work and i need some help on this 此功能无法正常工作,我需要一些帮助

thanks in advance... 提前致谢...

I just setup a sample scroll listening application here : 我只是在这里设置了一个示例滚动侦听应用程序:

http://jsfiddle.net/E2wj2/2/ http://jsfiddle.net/E2wj2/2/

You need to listen to scroll event for your scrollbox , 您需要监听滚动scrollbox滚动事件,

$('#scrollbox').scroll(function(){
  console.log('scrolling');
  scrolltop=$('#scrollbox').attr('scrollTop');
  if(scrolltop>=(scrollheight-(windowheight+scrolloffset))) {
    //do whatever you wish to do
  }
});

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM