实时代码不更新 - Js请求

时间:2018-01-24 22:04:05

标签: javascript php jquery json laravel

开发人员工具中的网络显示正在进行选择并让我预览正确的代码,但是当更改选择选项时,实际的实时代码不会更新,有关为什么会发生这种情况的任何想法以及如何解决问题,以便实时代码更改?

PHP:

   $batsmenQuery = Batsmen::where('approved', '=', 1);

   switch ($request->SortbyList){
        case 0:
            $batsmenQuery = $batsmenQuery->orderBy('name', 'DESC');
            break;
        case 1:
            $batsmenQuery = $batsmenQuery->orderBy('name', 'ASC');
            break;
        case 2:
            $batsmenQuery = $batsmenQuery->orderBy('hs', 'ASC');
            break;
        case 3:
            $batsmenQuery = $batsmenQuery->orderBy('hs', 'DESC');
            break;
            default:
                $batsmenQuery = $batsmenQuery->orderBy('name', 'DESC');


    }

    $batsmen= $batsmenQuery->paginate(40);

HTML:

   <div class="row">
        <div class="sort">
            <select name="SortbyList" id="SortBy">
                <option value="0">A to Z</option>
                <option value="1">Z to A</option>
                <option value="2">Highest Score</option>
                <option value="3">Lowest Score</option>

            </select>
        </div>
    </div>

JavaScript的:

 $(document).ready(function() {


     $('#SortBy').on('change', function(e) {
         $.ajax({
             url: "{{route('search.index')}}", // This is the url you make the request
             data: {SortbyList: this.value}, // Here you can send to the server the info you want in this case is only the value for the selected item
             dataType: "json",
             success: function (result) {

                 if (result) {
                     $("#SortBy").empty();  //This erase all the preview values
                     var new_options = '';
                     //This loop create the new values
                     $.each(result, function (k, v) {
                         new_options += '<option value="' + v.value + '">' + v.name + '</option>'
                     });
                     //Now we have all the values we can put on the select
                     $("#SortBy").append(new_options);


                 }

             }
         });
     });

0 个答案:

没有答案