简体   繁体   中英

Hide a div when ajax returns nothing

I have an ajax function that loads 1 news at time everytime the user clicks on "load more". It works like a facebook pagination.

The problem is: when it reachs the end of my database, it doesn't load anything else so I would like to remove the link "load more" when it happens but I couldn't do it.

Here's my JS function:

function loadNews(pageLimit){
         var dataString = 'pageLimit='+ pageLimit;
         $.ajax({
                type: "POST",
                url: "ajaxnovidades.php",
                data: dataString,
                cache: false,
                success: function(result){ 
                $("#maisnews").hide();
                $("#maisupdates").append(result);
          }
      });
    }
    loadNews('0');

I have tried to check if result is empty (result=="") and then hide() the #maisnews div, which is the one with the "load more" link but it doesn't work...

Any help?!

尝试这个:

if ( ! $.trim(result).length ) $('#maisnews').hide();

You can try something like:

function loadNews(pageLimit){
     var dataString = 'pageLimit='+ pageLimit;
     $.ajax({
            type: "POST",
            url: "ajaxnovidades.php",
            data: dataString,
            cache: false,
            success: function(result){ 
              if($.trim(result).length==0){
                //$(element).hide()
                $("#maisnews").hide();
              }else{
                $("#maisupdates").append(result);
              }
      }
  });
}
loadNews('0');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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