如何在滚动函数上运行ajax脚本?

时间:2015-02-20 21:19:43

标签: php jquery ajax function scroll

在滚动时我想运行Ajax脚本。以下是我想要使用的scroll函数和ajax脚本。我尝试过自己做的但我没有找到任何好办法。

<script src="https://code.jquery.com/jquery-1.7.2.js"></script>

<script>
$(document).ready(function() {
alert("I am Ready");
$(window).scroll(function() {
alert("I am scrolling");   // I want to use following Ajax here
})
})


function listtitle(){
$.ajax({
type: "POST",
url: "ramesh/index.php",
success:
function(result){
        $( "#ramesh" ).append(result);
    }
});}

</script>

2 个答案:

答案 0 :(得分:0)

你有jQuery作为$?

<script>

(function($){
 $(document).ready(function() {
  alert("I am Ready");
  $(window).scroll(function() {
   alert("I am scrolling");   // I want to use following Ajax here
  })
})


 function listtitle(){
 $.ajax({
 type: "POST",
 url: "ramesh/index.php",
 success:
 function(result){
        $( "#ramesh" ).append(result);
 }
 });}
})(jQuery);

</script>

答案 1 :(得分:0)

尝试以下方法:

$(document).ready(function() {
    alert("I am Ready");
    $(window).scroll(function() {
        listtitle();        // I want to use following Ajax here. SO I PLACED IT HERE
        alert("I am scrolling");
    });
});


function listtitle() {
    alert("in ajax");
    $.ajax({
        type: "POST",
        async: false, //SO THAT IT WAITS FOR THE LOAD BEFORE CONTINUING
        url: "ramesh/index.php",
        success: function(result) {
            $("#ramesh").append(result);
        }
    });
}