Laravel jQuery的问题是Uncaught TypeError:无法读取未定义的属性(阅读“值”)

wlp8pajw  于 2023-06-29  发布在  jQuery
关注(0)|答案(1)|浏览(136)
Uncaught TypeError: Cannot read properties of undefined (reading 'value')
    at $.<computed>.<computed>.menufocus (jquery-ui.js:5911:50)
    at HTMLUListElement.handlerProxy (jquery-ui.js:626:7)
    at HTMLUListElement.dispatch (jquery-3.6.0.js:5430:27)
    at elemData.handle (jquery-3.6.0.js:5234:28)
    at Object.trigger (jquery-3.6.0.js:8719:12)
    at HTMLUListElement.<anonymous> (jquery-3.6.0.js:8797:17)
    at Function.each (jquery-3.6.0.js:385:19)
    at jQuery.fn.init.each (jquery-3.6.0.js:207:17)
    at jQuery.fn.init.trigger (jquery-3.6.0.js:8796:15)
    at $.<computed>.<computed>._trigger (jquery-ui.js:715:16)

'我在刀片视图中的代码如下:'

<script>

$( "#searchName" ).autocomplete({
    source: "{{ route('item.autoSrchItem') }}",
    minLength: 2,
    autoFocus: true,
    Select: function(e, ui){
    console.log(ui.item);
  }
});
</script>

My Code in Controller is as:

public function autoSrchItem(Request $request)
{
   $search = $request->term;
   $result = Item::where('title', 'LIKE', '%'. $search. '%')->get();
   $data = [];
       foreach ($result as $key => $value) {
    $data [] = ['id'=>$value->id, 'title'=>$value->title];
   } 
      return response()->json($data);
 }


'我的浏览器图像:'

谁来帮帮我。我不明白有什么问题。我是Laravel的新手。当我给予源一个手动数据。它的工作完美,但当我给予源到我的数据表,它显示了上述大厦错误。

z3yyvxxp

z3yyvxxp1#

根据jQuery autocomplete documentationsource应该是array
路由方法route('item.autoSrchItem')将显示类似于...youwebsite/autoSrchItem的内容,这只是URL路径,而string将显示到source
在您的情况下,您需要更改代码以调用方法,并且不显示URL路径source: "{{ \PathToController\YourController::autoSrchItem() }}",,并在控制器中将此方法设置为static
这是一个JS代码的例子:

<script>
$( "#searchName" ).autocomplete({
    source: "{{ \PathToController\YourController::autoSrchItem("search_param") }}",
    minLength: 2,
    autoFocus: true,
    Select: function(e, ui){
    console.log(ui.item);
  }
});
</script>

对于控制器:

public static function autoSrchItem($search_param)
{
   $result = Item::where('title', 'LIKE', '%'. $search_param . '%')->get();
   $data = [];
   ...

这是静态自动完成列表的方法,如果你想让它是动态的,根据输入的字母,你需要改变代码的逻辑,也许这个stackoverflow question是解决方案。

相关问题