.ajax在事件处理程序中不起作用

时间:2016-10-03 04:12:31

标签: php ajax laravel

任何人都可以帮忙吗?我似乎无法发现我的代码存在任何问题。看来我的视图无法转到ReplacementDriverListDatabase.php 以下是我的观点

@section('libraries')
@parent
{!!Html::script('bootstrap-datepicker-1.5.1-dist/js/bootstrap-datepicker.min.js')!!}
{!!Html::style('bootstrap-datepicker-1.5.1-dist/css/bootstrap-datepicker.css')!!}
{!!Html::script('jonthornton-jquery-timepicker-fb09cd8/jquery.timepicker.js')!!}
{!!Html::style('jonthornton-jquery-timepicker-fb09cd8/jquery.timepicker.css')!!}
{!!Html::script('jonthornton-Datepair.js-efff9ba/dist/datepair.js')!!}
{!!Html::script('jonthornton-Datepair.js-efff9ba/dist/jquery.datepair.js')!!}
@stop
<div class="input-daterange form-group" id="datepicker">
{!! Form::text('replacementStartDate',NULL,['class'=>'input-sm form-control','id'=>'start','name'=>'replacementStartDate']) !!}
</div>

<div class="form-group">
    {!! Form::label('currentDriver','Current Driver') !!}
    {!!  Form::text('currentDriverList', NULL,['class'=>'form-control', 'id' => 'currentDriv']) !!}
</div>
@section("script")
<script>
  $("#start").select(function(){
    test(); 
    });
  function test(){
    $.ajax({
            url: '../resources/views/ReplacementDriverListDatabase.php',
            async: false,
            dataType:   "json", // <== JSON-P request
            success:    function(data){
                $.each(data.driverList, function(key, value){ // <=== Note, `data.results`, not just `data`
                $("#currentDriv").val('aaa');
                });
            }
  });
};
</script>
@stop

下一部分是我的ReplacementDriverListDatabase.php

<?php


 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "bustracking";


 $conn = new mysqli($servername, $username, $password, $dbname);

 $query = "SELECT driverNickName FROM driver";
 $result = $conn->query($query);

 $numberOfRow = $result->num_rows;

 $driverList["driverList"] = array();
 if ($numberOfRow > 0) {

  while ($row = $result->fetch_assoc()) {
    $dataRow = array();
    $dataRow['driverNickName'] = $row['driverNickName'];

    array_push($driverList["driverList"], $dataRow);
 }
 $result->free();
  }

 echo(json_encode($driverList));

 $conn->close();
 ?>

我在测试时没有显示任何错误。请帮忙。提前谢谢!

1 个答案:

答案 0 :(得分:0)

使用document.ready事件

$(document).ready(function(e) {
 $("#start").select(function(){
    test(); 
    });    
});