Laravel中的Ajax搜索功能-页面加载

时间:2018-11-19 07:07:10

标签: php jquery laravel

我是Laravel的新手。我正在使用ajax进行搜索。脚本和函数运行良好,但是在显示搜索结果时会重新加载整个页面,并且不显示div。它将重新加载整个页面。

我只给出了要显示的表ID,但它会加载整个页面。为什么会这样呢?错误在哪里做?谁能找到它并提供解决此问题的方法。

我的页面加载如下:

<script>
  $(document).ready(function(){
  $("button").on('click',function(e){
  e.preventDefault();
   var str=  $("#search").val();
   if(str == "") {
  $( "#tabledata" ).html( data );   
  } else {
  $.get( "{{ url('create?id=') }}"+str, 
  function( data ) {
   $( "#tabledata" ).html( data );   
   });
    }
     });  
      }); 
    </script>

搜索功能:

<div class="panel">
  <br>
    <div>
      <h2 style="font-family:Garamond;color:black;font-weight:bold;margin-left:5%; font-size:40px;"> 
    </div>
    <div class="col-lg-12" >
      <form class="navbar-form navbar-right" method="get">
        <div class="form-group">
          <input type="text"  class="form-control" id="search" placeholder="Search Here.." value="{{ request('search') }}">
        </div>
        <button type="submit" id="search" class="btn btn-default">Submit</button>
      </form>
    </div>

    <div class="container"  style=" max-width: 1150px;" >
      <table class="table table-bordered" style=" margin-top: 60px; font-family:timesnewroman">
        <thead class="text-justify">
        <tbody class="table-striped" id="tabledata">
          <tr class="table-info" style="font-weight:bold;font-size:20px;">
            <th class="text-justify"><strong> Name </strong></th>
            <th class="text-justify"><strong> Email</strong></th>
            <th class="text-justify"><strong> PhoneNumber</strong></th>
            <th class="text-justify"><strong> Action </strong></th>
          </tr>
          @foreach($users as $user)
          <tr class="table-info" style="font-weight: bold ;font-size:15;font-family:timesnewroman;background-color:#f9f9f9">
            <td>{{$user->name}}</td>
            <td>{{$user->email}}</td>
            <td>{{$user->phno}}</td>
            <td>
              <a href="{{ route('show',$user->id) }}">
                <span class="glyphicon glyphicon-eye-open" style=" color:#00a0df"></span>
              </a> &nbsp;
              <a href="{{ route('edit',$user->id)}}">
                <span class="glyphicon glyphicon-pencil" style=" color:green">
              </a>&nbsp;
              <a href="{{ route('delete',$user->id) }}"  onclick="return confirm('Are you sure to delete?')">
                <span class="glyphicon glyphicon-trash" style=" color:red"></span>
              </a>
            </td>
          </form>  
        </tr>
        @endforeach
      </tbody>
      </thead>
    </table>

控制器搜索功能:

public function search(Request $request)
{
  $search = $request->id;
  if (is_null($search)) {
    return view('admin.datas.create');        
  } else {   
    $users = data::where('name','LIKE',"%{$search}%")
                   ->get();
    return view('admin.datas.create',compact('users'));
        // return view('demos.livesearchajax')->withPosts($posts);
  }
}

1 个答案:

答案 0 :(得分:1)

好。所以问题出在你的jQuery上

解决方案1:

首先将id添加到您的表单中:

<form id="search-form" class="navbar-form navbar-right" method="get"> 

       // id is added on above line

      <div class="form-group">
       <input type="text"  class="form-control" id="search" placeholder="Search Here.." 
             value="{{ request('search') }}">
     </div>
     <button type="submit" id="search" class="btn btn-default">Submit</button>
  </form>

对于jquery使用:

<script>
    $(document).ready(function(){
    $("#search-form").on('submit',function(e){  // Use form submit event
      e.preventDefault();
      var str=  $("#search").val();
      if(str == "") {
         $( "#tabledata" ).html( data );   
       } else {
          $.get( "{{ url('create?id=') }}"+str, 
          function( data ) {
             $( "#tabledata" ).html( data );   
         });
        }
     });  
  }); 
</script>

解决方案2:

将按钮类型从submit更改为button,以便不提交表单

<form class="navbar-form navbar-right" method="get">
 <div class="form-group">
   <input type="text"  class="form-control" id="search" placeholder="Search Here.." 
         value="{{ request('search') }}">
 </div>
 <button type="button" id="search" class="btn btn-default">Submit</button>

 // type changed on above line

不要在button元素上使用click事件,它将绑定到当前页面上的所有按钮 使用“ #search” ID绑定点击事件

<script>
$(document).ready(function(){
   $("#search").on('click',function(e){  // Use button id to bind event