简体   繁体   English

从动态选择中运行Ajax,并选择从DB中检索的选项

[英]Run Ajax from a dynamic select with option selected retrieved from DB

I have the following select with options retrieved from Database 我有以下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>

I don't know how to run ajax from the selected option "Shared Tour" when the page is loaded. 我不知道如何在加载页面时从所选选项“共享游览”运行ajax。 With the change function as follows user has to change and then reselect Shared Tour to get the ajax response, I need this onload instead. 使用如下更改功能,用户必须更改然后重新选择共享游览以获取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);
    }
    });

    });

}); 

Thanks 谢谢

Not tested, but that you need is something like: 没有经过测试,但您需要的是:

$(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);
      }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM