【发布时间】:2017-11-18 01:08:27
【问题描述】:
如果我将结果附加到显示的正文中,我无法在 ID 为“results”的 div 中输出搜索结果,但是当我将其更改为 #results 时,它永远不会出现。
搜索框在 master.blade.php 的导航中,其他所有页面都包含它。
master.blade.php
<form id="search_form" action="/search" method="get" autocomplete="off" class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" id="search_text" onkeyup="search_data(this.value);" placeholder="Search">
</div>
<div id="result">
<table style="width:100%">
@if (isset($results) && count($results) > 0)
@foreach( $results as $business )
<tr>
<td>{{ $business->logo }}</td>
<td>{{ $business->name}}</td>
</tr>
@endforeach
@endif
</table>
</div>
</div>
</form>
控制器:
public function search($search_value) {
$search_text = $search_value;
if ($search_text==NULL) {
$data= Business::all();
} else {
$data=Business::where('name','LIKE', '%'.$search_text.'%')->get();
}
//$data = $data->toArray();
return view('master')->with('results',$data);
}
阿贾克斯:
function search_data(search_value) {
$.ajax({
url: '/searching/' + search_value,
type: 'post',
dataType: 'html',
success: function(data) {
$('#results').append(data);
},
error: function(data) {
$('body').html(data);
},
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}
});
}
路线::
Route::post('/searching/{search}', 'SearchController@search');
如果我真的查看 ajax 响应,结果就在那里:
<table style="width:100%">
<tr>
<td></td>
<td>dasd</td>
</tr>
</table>
但它永远不会出现在页面上!如果追加更改为正文,它会这样做。
//编辑
我有一个加载 gif 效果很好,但现在它永远不会消失,每次执行 ajax 时,它也不想消失:
$(document).ajaxStart(function() { Pace.restart(); });
$(window).on('load', function () {
setTimeout(function(){
$('.se-pre-con').fadeOut('slow', function () {
$(".container").css({ opacity: 1.0 });
});
}); // set the time here
});
$(document).ready(function() {
setTimeout(function() {
$(".messages").fadeOut(5000);
});
setTimeout(function() {
$(".messages").fadeOut(5000);
});
});
css:
.se-pre-con {
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
opacity: 0.5;
background-color: #6d6968;
background: url(../images/default.gif) center no-repeat #fff;
}
【问题讨论】:
-
重命名 id到并且在 ajax 成功:function(data) { $('#results').append(data) ; -> 替换为 $('#show_search_result').append(data); },为什么会这样?为什么结果不起作用我认为您遇到此问题是因为“结果”ID 在同一页面中使用超过一次。先检查一下。是的,确实如此,请查看我的编辑