2016-10-03 63 views
1

任何人都可以幫忙嗎?我似乎無法找到與我的代碼有任何問題。這似乎是我的觀點不能去到ReplacementDriverListDatabase.php 下面是我的看法.ajax在事件處理程序中不起作用

@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(); 
?> 

當我測試它它不顯示任何錯誤。請幫忙。提前致謝!

回答

0

使用的document.ready事件

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

THX您的回覆,但它仍然無法正常工作,這似乎是選擇事件處理程序可以繼續測試(),但$就不能工作。 – Alex

+0

使用錯誤事件捕捉錯誤 '功能測試(){$ 阿賈克斯({ 網址: '../resources/views/ReplacementDriverListDatabase.php', 異步:假, 數據類型: 「JSON」,//<== JSON-P請求 成功:函數(數據){.oach(data.driverList,function(key,value){// <===注意,'data.results',不只是'data ' $( 「#currentDriv」)VAL( 'AAA'); });} , \t \t \t錯誤:函數(XHR,狀態,錯誤){ \t \t \t \t var err = eval(「(」+ xhr.responseText +「)」); \t \t \t \t alert(err.Message); \t \t \t} }); }; ' – Asim

+0

它顯示SyntaxError:意外的標記<位於JSON位置0 @Asim – Alex