Javascript 在Laravel-5.1中使用引导式Typeahead
我正在尝试在Laravel-5.1版本中实现这个纯PHP项目(),但我还没有弄清楚需要做什么。引导Typeahead不受影响。如果有人指点我该怎么做,我会很感激的 这是刀片的形式:Javascript 在Laravel-5.1中使用引导式Typeahead,javascript,php,json,laravel-5.1,bootstrap-typeahead,Javascript,Php,Json,Laravel 5.1,Bootstrap Typeahead,我正在尝试在Laravel-5.1版本中实现这个纯PHP项目(),但我还没有弄清楚需要做什么。引导Typeahead不受影响。如果有人指点我该怎么做,我会很感激的 这是刀片的形式: {!! Form::open(array('url' => 'created-products', 'method' => 'post'), array('id'=>'createproduct')) !!} {!! Form::token() !!} <link h
{!! Form::open(array('url' => 'created-products', 'method' => 'post'), array('id'=>'createproduct')) !!}
{!! Form::token() !!}
<link href="{!! asset('bootstrap/css/bootstrap.css') !!}" media="all" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="style.css">
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<p>{!! Form::select('countries', array('-1' => 'Select a Country') + $listedCountries) !!} </p>
<p>{!! Form::text('typeahead', null, array('class'=>'well', 'placeholder'=>'enter your state'), array('id'=>'selectedCompanyHiddenId', 'data-provider'=>'typeahead')) !!}</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="{!! asset('bootstrap/js/bootstrap.js') !!}" media="all" rel="stylesheet" />
<script>
$(function(){
$('#typeahead').typeahead({
source: function(query, process){
$.ajax({
url: 'UserAddressController.php?SelectLocationPlaces',
type: 'POST',
data: 'query=' +query,
dataType: 'JSON',
async: true,
success: function(data){
process(data);
}
});
}
});
});
</script>
{!! Form::close() !!}
返回记录的方法:
public function SelectLocationPlaces()
{
if(isset($_POST['query']))
{
$query = $_POST['query'];
$locationPlaces = LocationPlaces::where('name', 'like', "%{$query}%")->toArray();
return json_encode($locationPlaces);
}
}
首先,你需要提出正确的问题。为什么你认为它不起作用?i、 你做什么,你期望得到什么,你实际拥有什么。我所做的就是这段代码。我所期望的是,当我聚焦id=typeahead的文本框并键入任何字符时,它应该根据输入的内容选择匹配的记录。由于typeahead使用模糊搜索,从显示的匹配记录中,用户只需选择填充文本框,而无需实际键入新值。谢谢期待您的输入。您的AJAX URL直接指向您的控制器文件,我认为这不正确。你有去那里的路线吗?看看JS控制台的错误。如果我不够清楚,很抱歉,但我问你作为一个用户,而不是作为一个开发人员做什么。问题太多,无法立即解决。我建议首先修复后端,然后修复前端:从
curl
向您的控制器发送POST请求-这是您作为客户机所做的;观察输出——它是你实际得到的;将它与必须返回到前端的内容进行比较——这就是您所期望的。将这三条信息添加到问题中,得到一个直接的答案。
public function SelectLocationPlaces()
{
if(isset($_POST['query']))
{
$query = $_POST['query'];
$locationPlaces = LocationPlaces::where('name', 'like', "%{$query}%")->toArray();
return json_encode($locationPlaces);
}
}