Laravel - AJAX搜索适用于输入但不适用于选择

时间:2017-08-05 19:50:38

标签: javascript php jquery ajax laravel

我遇到了一些问题。我正在尝试为我的数据库创建一个过滤器。到目前为止,我已经想出了这个:

刀片文件:

<input type="text" class="form-control" id="search" name="search" placeholder="Name, NORAD or ID"></input>

<select name="filtername" id="filtername">
    <option value="none"></option>
    <option value="Falcon">Falcon</option>
    <option value="rb">R/B</option>
</select>

Javascript AJAX致电:

$(document).ready(function() {
$('#search').on('keyup', function() {
    $value = $(this).val();
    delay(function() {
        if ('#search'.length > 3) {
            $.ajax({
                type: 'get',
                url: '{{$launchsitename->site_code}}',
                data: {
                    'search': $value
                },
                success: function(data) {
                    $('#launchsatdisplay').html(data);
                }
            });
        }
    }, 300);
});

$("#filtername").change(function() {
    var filtername = $(this).val();
    var dataString = "filtername=" + filtername;
    $.ajax({
        type: "get",
        url: "{{$launchsitename->site_code}}",
        data: {
            'search': dataString
        },
        success: function(data) {
            $('#launchsatdisplay').html(data);
        }
    });
});
});

控制器:

if ($request->ajax())
    {
        $output="";
        $launchsitesatellite = DB::table('satellites')
            ->where(function($q) use ($request) {
                $q->orWhere('satname','LIKE','%'.$request->search.'%')
                  ->orWhere('norad_cat_id','LIKE','%'.$request->search.'%')
                  ->orWhere('object_id','LIKE','%'.$request->search.'%');
            })
            ->where('site', $site_code)->get();
        if ($launchsitesatellite)
        {
            $output .='<tr>'.
            '<th>'.'Satellite Name'.'</th>'.
            '<th>'.'NORAD ID'.'</th>'.
            '<th>'.'Object Type'.'</th>'.
            '<th>'.'Launch Date'.'</th>'.
            '<th>'.'Country'.'</th>'.
            '<th>'.'Object ID'.'</th>'.
            '<tr>';
            foreach ($launchsitesatellite as $key => $launchsitesatellites) {
            $output .='<tr>'.
                    '<td>'.$launchsitesatellites->satname.'</td>'.
                    '<td>'.$launchsitesatellites->norad_cat_id.'</td>'.
                    '<td>'.$launchsitesatellites->object_type.'</td>'.
                    '<td>'.$launchsitesatellites->launch.'</td>'.
                    '<td>'.$launchsitesatellites->country.'</td>'.
                    '<td>'.$launchsitesatellites->object_id.'</td>'.
                    '</tr>';
            }
        }
        return $output;
    }
    else {
        $launchsitesatellite = DB::table('satellites')->where('site', $site_code)->Paginate(40);
        return view('pages/launchsite-filter', compact('launchsites', 'launchsitesatellite'));
    }

此代码用于对我的控制器进行AJAX调用并从数据库中获取结果。上面发布的代码适用于input。我能够轻松搜索数据库。唯一的问题是select。每当我选择一个选项时,它都不会返回任何结果。我猜我在控制器中编程select的方式有问题,因为我不认为Javascript会导致问题。

select应该在我的数据库satname中读取satellites列并返回过滤后的结果。

有人能够解释为什么select不起作用或我的代码中的错误在哪里?

1 个答案:

答案 0 :(得分:0)

修改选择框的ajax调用代码,如下所示

$("#filtername").change(function() {

    var dataString = $(this).val();
    $.ajax({
        type: "get",
        url: "{{$launchsitename->site_code}}",
        data: {
            'search': dataString
        },
        success: function(data) {
            $('#launchsatdisplay').html(data);
        }
    });
});