Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

laravel - How to show blank if no search results found through Ajax?

I am trying to search similar posts through Ajax while typing in input. And I want to get the blank div if there are no results. Here is my Ajax code I have tried:

$('#title').on('keyup',function(){
   $value=$(this).val();
       $.ajax({
          type : 'get',
          url : '{{URL::to('similarpost')}}',
          data:{'search':$value},
          success:function(data){
             if(data != null){
                 $('.mydiv').html(data);
             }
             else{
                 $('.mydiv').html(); //Here I want the div to be blank but it's not working
             }
          }
       });
 })                    
question from:https://stackoverflow.com/questions/65950201/how-to-show-blank-if-no-search-results-found-through-ajax

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try like this, if you want the div to be blank, put like this $('.mydiv').html('');

$('#title').on('keyup',function(){
   let $value = $(this).val();
       $.ajax({
          type : 'get',
          url : '{{URL::to('similarpost')}}',
          data:{'search':$value},
          success:function(data){
             if(data != null){
                 $('.mydiv').html(data);
             }else{
                 $('.mydiv').html('');
             }
          }
       });
 })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...