我有一个选择框,它会改变我从数据库返回的结果的顺序,具体取决于所选择的选择框值,该值与交换机PHP语句相关联,我遇到的问题是我的JavaScript没有尽管在选择选项更改时在开发人员预览中显示了JSON上的已知错误,但是刷新我的结果以匹配所选的switch语句,但是在控制台中抛出了以下错误,我认为这是停止更新的结果与所选的选项有关,有关如何修复此JSON错误的任何想法?
未捕获TypeError:无法使用'in'运算符在错误中搜索'50602'(...)
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);
}
}
});