我有一个数组,在该数组中,我正在从一个API端点中删除一个stdClass对象:
foreach($searchResults->hits as $arr){
foreach ($arr as $obj) {
$fullType = $obj->_source->categories;
print_r($fullType);
}
}正确地为我返回一个正确的列表。问题是,我在这里使用ha rdcoded值"testProduct“查询端点:
$results = "testProduct";
$searchResults = $service->getSearch($results);因此,这将返回在整个对象中具有类似于testProduct的产品列表。
我的问题是,我试图用一个输入值替换硬编码的值。我的前端有一个输入:
<form class="uk-search" data-uk-search>
<input class="uk-search-field" type="search" placeholder="search products...">
</form>我试图在这里执行一个自动完成函数,以便当用户在输入中输入时,我将运行$searchResults,并且我将把上面的$fullType放在输入的结果列表中。
我该怎么做呢?
更新:
当我输入输入时,我的控制台为每次击键都打印成功,这样我就知道post是正确的。但是,我应该如何使它返回$searchResults的结果呢?如果我想要console.log $searchResults的每一个击键的电话?
Controller.php
public function autoComplete(Request $request)
{
$search_result = $request->search_result;
$service = new service();
$searchResults = $service->getSearch($search_result);
}view.blade.php
<script type="text/javascript">
$('#productInput').on('input', function(){
if($(this).val() === ''){
return;
}else{
const searchResult = $(this).val();
$.ajax({ url: '/account/autocomplete',
data: {
'search_result':searchResult
},
type: "POST",
success: function(response){
console.log("success");
}
});
}
});
</script>发布于 2018-08-28 12:07:13
要使用JQuery将input事件处理程序添加到输入框中:
//Place this in the $(document).ready() of your script
//Adds the event handler for input of the textbox
$('.uk-search-field').on('input', function(){
//If there is no input yet, or they deleted the text, don't do anything
if($(this).val() === ''){
return;
}else{
//Call your server side method here, first get the value of the input box
const searchValue = $(this).val(); //"this" refers to the input box at this point
//to use ajax, see below
$.ajax({ url: "yourServersideUrl",
type: "POST",
data: { serverSideMethodParameterName: searchValue}, //Data to pass to server
success: function(data){ //Deserialize data and populate drop down },
error: function(jqXHR, exception){ //Handle exception }
}});
}
});https://stackoverflow.com/questions/52046505
复制相似问题