【发布时间】:2018-01-25 01:34:22
【问题描述】:
我有一个选择框,它根据选择的选择框值更改从数据库返回的结果的顺序,该选择框值链接到 switch 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);
}
}
});
【问题讨论】:
-
Javascript 错误?无聊的 HTTP 请求?您可以在浏览器开发工具中检查这两个。
-
请检查控制台是否有任何错误
-
未捕获的类型错误:无法使用“in”运算符在中搜索“50602”
-
猜测它看起来像 SQL 错误,因为单词 in。检查您的查询并尝试调试它是否运行正常。
-
不,我认为 .ajax 有错误,但不确定故障或如何修复
标签: javascript php jquery ajax laravel