【问题标题】:How to reload page on select option Ajax get method如何在选择选项 Ajax get 方法上重新加载页面
【发布时间】:2021-04-02 08:57:33
【问题描述】:

我的选择选项中有一个比赛列表,

我需要知道如何让这个功能发挥作用,

当用户选择比赛时,页面应重新加载比赛数据,

这是我的刀片中的选择选项

<div class="row pt-3">
    <div class="col">
         <select name="competition" id="competitionSearch" class="select listing pt-2 pb-2" >
               <option value="">Select Competition</option>
                   @foreach ($allCompetitions as $competition)
                      <option value="{{ $competition->id }}"> {{ $competition->name }}</option>
                   @endforeach
          </select>
    </div>
</div>

这就是它的样子

这是我用过的ajax get方法

 $(document).ready(function(){
  
    $('#competitionSearch').change(function(){
        
        var compid = $(this).val();
            if(compid > 0){
                fetchRecords(compid);
            }
    });
});

function fetchRecords(id){
$.ajax({
    url: 'detailed-registrations/getCompetitionAjax/'+id,
    type: 'get',
  
    success: function(response){
        if (response) {
            //load selected competition here
        }
    }
});
}

我的路线

Route::get('competitions/{competition}/detailed-registrations/getCompetitionAjax/{id}','CompetitionController@getCompetitionAjax');

我在控制器中的功能

public function getCompetitionAjax($competition, $id, Request $request)
{
   $comp = $this->competitionsRepo->findOrFail($id);

    return redirect(route('competitions.detailed-registrations',$id))->with('comp',$comp);
}

我需要知道如何让这个功能发挥作用

最初用户会在一个竞赛页面中

http://127.0.0.1:8000/competitions/1/detailed-registrations

一旦他选择了比赛 2 就重定向到比赛页面,过滤后的查询参数为

http://127.0.0.1:8000/competitions/2/detailed-registrations?filters=visible&age=all&gender=all

【问题讨论】:

  • 你应该为getCompetitionAjax方法返回json并在js中使用响应

标签: php ajax laravel parameters laravel-7


【解决方案1】:

否认为您需要一个ajax调用将用户重定向到不同的页面时选择竞争,您可以从JavaScript中重定向到

$(document).ready(function(){
  
    $('#competitionSearch').change(function(){
        
        var compid = $(this).val();
            if(compid > 0){
                //Redirect the user to the page which will show the selected details
                location = 'competitions/' + id + '/detailed-registrations?filters=visible&age=all&gender=all';
            }
    });
});

然后让 Laravel 控制器方法处理请求到路由 competitions.detailed-registrations

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    相关资源
    最近更新 更多