Javascript 选择选项在更改时从 ajax 生成值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28059029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:09:53  来源:igfitidea点击:

select option generate value from ajax on change

javascriptphpjqueryajaxselect

提问by Brownman Revival

I have herethe code and flow of my project i have 3 select here one for continent one for country one for city i get data to populate these select from ajax request it is now working fine i just want to make a bit fancy so i want to have a few function

我在这里有我的项目的代码和流程 我在这里有 3 个选择 一个用于大陆 一个用于国家 一个用于城市 我获取数据以填充这些来自 ajax 请求的选择 它现在工作正常我只是想变得有点花哨所以我想要有几个功能

1.When Continent is select the list of country for that continent is listed in the country list when the change happens I want the city to also show the cities of the first entry in the country currently it does not happened what i do is i still need to change the entry in country select to show the list of cities

1.当大陆选择该大陆的国家名单时在国家名单上列出了当发生变化时,我希望城市也展示该国的第一个进入的城市目前它不会发生我所做的是我仍然是我所做的需要更改国家选择中的条目以显示城市列表

2.Question is do i need to add another ajax request inside the ajax request for continent i am not sure this one is feasible i tried it, it is not working for now

2.问题是我是否需要在大陆的ajax请求中添加另一个ajax请求我不确定这个是否可行我试过了,它现在不起作用

Ajax Code

阿贾克斯代码

$('.continentname').change(function() {
        var id = $(this).find(':selected')[0].id;
        //alert(id); 
        $.ajax({
            type:'POST',
            url:'../include/continent.php',
            data:{'id':id},
            success:function(data){
                // the next thing you want to do 
    var country= document.getElementById('country');
              $(country).empty();
    var city = document.getElementById('city');
              $(city).empty();
    for (var i = 0; i < data.length; i++) {
    $(country).append('<option id=' + data[i].sysid + ' value=' + data[i].name + '>' + data[i].name + '</option>');
    }
            }
        });

    });

$('.countryname').change(function() {
        var id = $(this).find(':selected')[0].id;
        $.ajax({
            type:'POST',
            url:'../include/country.php',
            data:{'id':id},
            success:function(data){
                // the next thing you want to do 
    var city = document.getElementById('city');
              $(city).empty();
    for (var i = 0; i < data.length; i++) {
    $(city).append('<option id=' + data[i].sysid + ' value=' + data[i].name + '>' + data[i].name + '</option>');
    }
            }
        });

    });

From database i put the value into the option select like

从数据库中,我将值放入选项 select like

$("#continent").val(continentid);
$("#continent").change();
$("#country").change();
$("#country").val(countryid);
$("#city").val(cityid);

采纳答案by Arun P Johny

You can trigger a change event for the country element once it is populated

您可以在填充 country 元素后触发更改事件

$('.continentname').change(function () {
    var id = $(this).find(':selected')[0].id;
    //alert(id); 
    $.ajax({
        type: 'POST',
        url: '../include/continent.php',
        data: {
            'id': id
        },
        success: function (data) {
            // the next thing you want to do 
            var $country = $('#country');
            $country.empty();
            $('#city').empty();
            for (var i = 0; i < data.length; i++) {
                $country.append('<option id=' + data[i].sysid + ' value=' + data[i].name + '>' + data[i].name + '</option>');
            }

            //manually trigger a change event for the contry so that the change handler will get triggered
            $country.change();
        }
    });

});

$('.countryname').change(function () {
    var id = $(this).find(':selected')[0].id;
    $.ajax({
        type: 'POST',
        url: '../include/country.php',
        data: {
            'id': id
        },
        success: function (data) {
            // the next thing you want to do 
            var $city = $('#city');
            $city.empty();
            for (var i = 0; i < data.length; i++) {
                $city.append('<option id=' + data[i].sysid + ' value=' + data[i].name + '>' + data[i].name + '</option>');
            }
        }
    });
});

回答by Bandon

You can get both country list and city list of first country in ../include/continent.php:

您可以在以下国家/地区获取第一个国家/地区的国家/地区列表和城市列表../include/continent.php

  1. Get country list array
  2. Get city list where countryid = country[0].id
  3. Combine them and add another field like type
  1. 获取国家列表数组
  2. 获取 countryid = country[0].id 的城市列表
  3. 组合它们并添加另一个字段,例如 type

Example:

例子:

type     | sysid | name
country  | 1     | America
country  | 2     | Canada
city     | 1     | New York
city     | 2     | Los Angles

Then in javascript:

然后在javascript中:

$.post("../include/continet.php", {"id":id}, function(data){
  $(country).empty();
  $(city).empty();
  for (var i = 0; i < data.length; i++) {
    if(data[i].type == "country"){
      $(country).append...
    }
    else{
      $(city).append...
    }
  }
});