更改选择值时没有任何事情发生

时间:2018-01-24 17:30:09

标签: javascript php jquery ajax laravel

我有一个选择框,它改变了从数据库返回的结果的顺序,这取决于所选择的选择框值,该值被链接到交换机PHP语句,问题是我的JavaScript似乎不起作用,任何关于我哪里出错的想法?​​

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的:

 $('#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
         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);
             }

         }
 });

1 个答案:

答案 0 :(得分:0)

您是否尝试将JS包装在

中?
$(document).ready(function() {


 });

我养成了随时通过jquery

将任何属性附加到对象的习惯