Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据laravel中的选定选项刷新页面_Javascript_Jquery_Ajax_Laravel - Fatal编程技术网

Javascript 如何根据laravel中的选定选项刷新页面

Javascript 如何根据laravel中的选定选项刷新页面,javascript,jquery,ajax,laravel,Javascript,Jquery,Ajax,Laravel,我是javascript新手,如何在选择laravel中的页数后刷新页面 我的控制器代码 $itemCount = Input::get('item_count'); $data = [ 'allItems' => $venueData->get(), 'priorityItems' => $venueData->take($itemCount)->get() ]; 我的视图代码 <div clas

我是javascript新手,如何在选择laravel中的页数后刷新页面

我的控制器代码

$itemCount = Input::get('item_count');

    $data = [
        'allItems'      => $venueData->get(),
        'priorityItems' => $venueData->take($itemCount)->get()
    ];
我的视图代码

<div class="form-group select-page">
    <select class="form-control" id="sel1" name="item_count">
        <option value="10" @if(Input::has('item_count') && Input::get('item_count') == '10') selected @endif>1</option>
        <option value="20" @if(Input::has('item_count') && Input::get('item_count') == '20') selected @endif>2</option>
        <option value="40" @if(Input::has('item_count') && Input::get('item_count') == '40') selected @endif>60</option>
        <option value="80" @if(Input::has('item_count') && Input::get('item_count') == '80') selected @endif>80</option>
        <option value="100" @if(Input::has('item_count') && Input::get('item_count') == '100') selected @endif>100</option>
    </select>
</div>

1.
2.
60
80
100

如何根据以下选项刷新页面并加载项目

$('#sel1').change(function(){
    var value = $(this).val();
    // here redirect to link with item_count value
});

实现如下

$('#sel1').change(function(){
    var value = $(this).val();
    // here redirect to link with item_count value
});
在你的行动中

public function your_action(Request $request)//inject request to access per_page query param
{
    $perPage = $request->query('per_page', 10);
    $items =  YourModel::paginate($perPage)->appends([
        'per_page'   => $perPage
    ]);

    return view('your_view', compact('items', 'perPage'));
}
现在在你的JS中

<div class="form-group select-page">
    <select class="form-control" id="sel1" name="item_count" onchange="window.location.href = window.location.href.replace('per_page={{ $perPage }}', '') + 'per_page=' + this.value">
        <option value="10" @if($perPage == '10') selected @endif>1</option>
        <option value="20" @if($perPage == '20') selected @endif>2</option>
        <option value="40" @if($perPage == '40') selected @endif>60</option>
        <option value="80" @if($perPage == '80') selected @endif>80</option>
        <option value="100" @if($perPage == '100') selected @endif>100</option>
    </select>
</div>

1.
2.
60
80
100
onchange
时,您正在运行脚本。当
window.location.href
设置为某个值时,您将重定向到url&这是在更改select的选项值时发生的

这里
window.location.href.replace('per_page={{$perPage}}','')
您实际上是在替换
per_page=10
per_page=20
每个页面的任何查询键值,以便它不会在url中重复

下一步
'per_page='+此.value
我们再次在您的Laravel操作中添加
per_page
值。

public function your_action(Request $request)//inject request to access per_page query param
{
    $perPage = $request->query('per_page', 10);
    $items =  YourModel::paginate($perPage)->appends([
        'per_page'   => $perPage
    ]);

    return view('your_view', compact('items', 'perPage'));
}
现在在你的JS中

<div class="form-group select-page">
    <select class="form-control" id="sel1" name="item_count" onchange="window.location.href = window.location.href.replace('per_page={{ $perPage }}', '') + 'per_page=' + this.value">
        <option value="10" @if($perPage == '10') selected @endif>1</option>
        <option value="20" @if($perPage == '20') selected @endif>2</option>
        <option value="40" @if($perPage == '40') selected @endif>60</option>
        <option value="80" @if($perPage == '80') selected @endif>80</option>
        <option value="100" @if($perPage == '100') selected @endif>100</option>
    </select>
</div>

1.
2.
60
80
100
onchange
时,您正在运行脚本。当
window.location.href
设置为某个值时,您将重定向到url&这是在更改select的选项值时发生的

这里
window.location.href.replace('per_page={{$perPage}}','')
您实际上是在替换
per_page=10
per_page=20
每个页面的任何查询键值,以便它不会在url中重复

下一步
'per_page='+此.value
我们再次添加
per_page