2017-01-24 23 views
0

我有以下select運行阿賈克斯從動態的選項選擇選定從數據庫中檢索

<select name="type_service" id="type_service" class="type_service"> 
    <option value="Airport Transfer">Airport Transfer</option> 
    <option value="Private Tour">Private Tour</option> 
    <option value="Shared Tour" selected="selected">Shared Tour</option> 
    <option value="Shore Trip">Shore Trip</option> 
    <option value="Port Transfer">Port Transfer</option> 
</select> 

我不知道如何從選擇的選項「共享之旅」時,阿賈克斯運行從數據庫中檢索選項該頁面已加載。使用如下更改函數用戶必須更改,然後重新選擇共享遊覽以獲取ajax響應,我需要這個onload。

$(document).ready(function(){ 
    $(".type_service").change(function(){ 
    var id=$(this).val(); 
    var dataString = "id="+ id; 

    $.ajax({ 
    type: "POST", 
    url: "ajax/ajax_type.php", 
    data: dataString, 
    cache: false, 
    success: function(html){ 
     $(".result").html(html); 
    } 
    }); 

    }); 

}); 

感謝

回答

0

沒有測試,但你需要的是這樣的:

$(document).ready(function() { 
ajax_request($("#type_service option:selected").val()); 

    $(".type_service").change(function() { 
    var id = $(this).val(); 
    ajax_request(id); 
    }); 

}); 
function ajax_request(id){ 
     var dataString = "id=" + id; 
    $.ajax({ 
     type: "POST", 
     url: "ajax/ajax_type.php", 
     data: dataString, 
     cache: false, 
     success: function(html) { 
     $(".result").html(html); 
     } 
    }); 
}