dongzhimeng2464 2018-04-27 06:49
浏览 64
已采纳

在laravel中提交表单后,请记住滚动条位置

Is it possible to remember scrollbar position after form submit and returning back to the home page?

Here is my form:

<form method="post" action="{{Route('sites.liking')}}">
  <input name="_token" value="{{csrf_token()}}" type="hidden">
  <input type="hidden" value="{{$meme->likes}}" name="likes">
  <input type="hidden" value="{{$meme->id}}" name="likeid">
  <input type="hidden" value="{{Auth::user()->name}}" name="sendeduser">
  <button class="linking">+ {{$meme->likes}}</button>
</form>

Routing:

Route::post('/', ['uses' => 'LikingComments@liking', 'as' => 'sites.liking']);

Controller function:

public function liking(Request $request) {
  //Function guts
  return redirect('/')->with('status', $id);
}

I've tried several scripts but it always scrolls to the top of the page.

Is there a some way to go back to the same place of the page ?

  • 写回答

2条回答 默认 最新

  • dsgwdigu84950 2018-04-27 08:11
    关注

    Okay I've solved up that problem by adding an id to an element, new routing and changing main function.

    <div id="{{$meme->id}}">
     <form method="post" action="{{Route('sites.liking')}}">
      <input name="_token" value="{{csrf_token()}}" type="hidden">
      <input type="hidden" value="{{$meme->likes}}" name="likes">
      <input type="hidden" value="{{$meme->id}}" name="likeid">
      <input type="hidden" value="{{Auth::user()->name}}" name="sendeduser">
      <button class="linking">+ {{$meme->likes}}</button>
     </form>
    </div>
    

    Changed function:

    public function liking(Request $request)
    {
     //Function guts
    
    
     return redirect()->action('LikingComments@indexliking', ['id' => $id])->with('status', $id);
    }
    

    Routing:

    Route::get('/#{id}' , ['uses' => 'LikingComments@indexliking', 'as' => 'indexliking']);
    

    It don't work perfectly but it's fine for me. Thank you for your answers especially @kerbholz

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?