【问题标题】:select option generate value from ajax on change选择选项在更改时从 ajax 生成值
【发布时间】:2015-03-19 11:44:16
【问题描述】:

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

1.当大陆选择该大陆列表,在国家名单上列出了当发生变化时,我想要城市 还要显示目前国内第一个入境的城市 它没有发生我所做的是我仍然需要更改条目 在国家/地区选择以显示城市列表

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

Ajax 代码

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

    });

从数据库中,我将值放入选项中,像这样选择

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

【问题讨论】:

    标签: javascript php jquery ajax select


    【解决方案1】:

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

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

    【讨论】:

    • 这很简单,我认为我需要制作另一个 ajax,这是浪费时间,我无法在接下来的 10 分钟内打勾,请放心,我会打勾,以便其他人可以检查答案
    • 先生,您对答案做了哪些修改?$('#country')document.getElementById('country') 不同吗?
    • @HogRider 因为您使用的是 jQuery....这是 jQuery 的方式...简单且更少的输入...也无需多次调用$(country)
    • 如何触发 onchange 事件而不更改当前数据。我问这个是因为当我从数据库加载信息时,它不会转到正确的信息,而是转到列表示例中的第一个条目,我需要在国家/地区列表中显示Japan,但它会显示第一个国家/地区亚洲 Afghanistan 与此示例的城市相同,我需要显示 Tokyo 它将显示 Afghanistan 的第一个城市 Chaghcharan
    • 先生,如果您有时间,请回答我最近的问题
    【解决方案2】:

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

    1. 获取国家列表数组
    2. 获取 countryid = country[0].id 的城市列表
    3. 组合它们并添加另一个字段,例如type

    例子:

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

    然后在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...
        }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2023-03-17
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多