douzhan1031 2015-02-27 19:47
浏览 60

Laravel5中的jQuery自动完成功能无法正常工作

This is the basic code. I cannot seem to get it to work in Laravel 5:

routes.php

Route::get('h2h', 'atp_players\H2hController@getIndex');
Route::get('h2h_getdata', 'atp_players\H2hController@getData');

H2hController.php

namespace Atpstats\Http\Controllers\atp_players;
use Atpstats\Http\Controllers\Controller;
use Response;
use Request;


class H2hController extends Controller{

public function getIndex() {
    return view('atp_players.h2h');
}

public function getData() {
    $term =  Request::input('auto', 'r');

    $results = \DB::table('atp_players')->select('firstname')->get();
    $data = array();

    foreach($results as $result) {

         if(strpos($result,$term) !== false) {
            $data[] = ['value' => $result->firstname];
        }
    }
    return Response::json($data);
}
}

View: h2h.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery UI Autocomplete test</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
<div class="container">
<div class="ui-widget">
    <label for="">Find a player</label>
    <input type="text" class="form-control input-sm" name="auto" id="auto"  autocomplete="on">
</div>
<div class="form-group">
    <label for="">Response</label>
    <input type="text" class="form-control input-sm" name="response" id="response" disabled>
</div>

<script>
    $('#auto').autocomplete({
        type: "get",
        source: 'h2h_getdata',
        dataType: "json",
        minLength: 1,
    select:function(e,ui){
    $('#response').val(ui.item.value);
}
});

</script>
</body>
</html>

It doesn't work. It's doing nothing event when you write something to the input text case. But if I comment some lines in Controller, the data (firstname) of the DB appear but autocomplete doesn't work.

And that's the modified Controller function:

public function getData() {
    //$term =  Request::input('auto', 'r');

    $results = \DB::table('atp_players')->select('firstname')->get();
    $data = array();

    foreach($results as $result) {

         //if(strpos($result,$term) !== false) {
            $data[] = ['value' => $result->firstname];
        //}
    }
    return Response::json($data);
}
  • 写回答

2条回答 默认 最新

  • dongtun1872 2015-02-27 20:22
    关注

    I think the problem may lay on server side code. Try changing the form item name from 'auto' to 'term', like this:

    ...
    public function getData() {
    $term =  Request::input('term', 'r');
    ...
    

    For more info check http://api.jqueryui.com/1.10/autocomplete/#option-source

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计