Codeigniter 中国家/地区和城市的 Ajax 下拉菜单?

2023-12-19

我正在我们的 Codeigniter 框架中借助 ajax 使国家和城市下降。

数据库的结构如下所示。

Country

country_id,country_name

State

country_id,state_id,state_name

city

country_id,state_id,city_id,city_name

用户控制器

function country(){
 $data['header']='Deal Management';
 $data['page'] = 'admin/page/user-view';
 $data['Country'] = $this->deal->getCountry(); 
 $this->load->view($this->_admin_container,$data);  
}

function get_cities($Country){
  $this->load->model('city_model');
  header('Content-Type: application/x-json; charset=utf-8');
  echo(json_encode($this->cities_model->get_cities($dealCountry)));
}

用户视图视图

<?php $cities['#'] = 'Please Select'; ?>

<label for="country">Country: </label><?php echo form_dropdown('country_id', $Country, '#', 'id="country"'); ?><br />

<label for="city">City: </label><?php echo form_dropdown('city_id', $cities, '#', 'id="cities"'); ?><br />

交易模块country_model

 function getCountry(){
    $this->db->select("*");
    $query=$this->db->get("deal_country");
    $countries = array();
    if($query->result()){
          foreach ($query->result() as $country) {
              $countries[$country->country_id] = $country->country_name;
          }
        return $countries;
        }else{
        return FALSE;
        }
    }

城市模型模块

function get_cities($dealCountry = null){
        echo $dealCountry;die;
      $this->db->select('city_id, city_name');

      if($dealCountry != NULL){
          $this->db->where('country_id', $dealCountry);
      }

      $query = $this->db->get('deal_city');

      $cities = array();

      if($query->result()){
          foreach ($query->result() as $city) {
              $cities[$city->id] = $city->city_name;
          }
      return $cities;
      }else{
          return FALSE;
      }
}

我在头文件中包含 ajax 脚本。

<script type="text/javascript">// <![CDATA[
    $(document).ready(function(){
        $('#country').change(function(){ //any select change on the dropdown with id country trigger this code
        $("#cities > option").remove(); //first of all clear select items
            var country_id = $('#country').val();  // here we are taking country id of the selected one.
            $.ajax({
                type: "POST",
                url: "home/get_cities/"+country_id, //here we are calling our user controller and get_cities method with the country_id

                success: function(cities) //we're calling the response json array 'cities'
                {
                    $.each(cities,function(city_id,city) //here we're doing a foeach loop round each city with id as the key and city as the value
                    {
                        var opt = $('<option />'); // here we're creating a new select option with for each city
                        opt.val(id);
                        opt.text(city);
                        $('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
                    });
                }

            });

        });
    });
    // ]]>
</script>

编写代码后它不起作用。

任何帮助表示赞赏!

Thanks


创建一个 JavaScript 变量

var base_url = "<?=base_url()?>";

在你的 javascript 代码中 concat 它

url: base_url+"home/get_cities/"+country_id, 

使用 firefox 和 firebug 调试 ajax 代码

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Codeigniter 中国家/地区和城市的 Ajax 下拉菜单? 的相关文章

随机推荐

  • React Native 测试 - 无需等待即可执行

    下面的测试通过了 但我收到以下警告两次 我不知道为什么 有人可以帮我弄清楚吗 console error Warning You called act async gt without await This could lead to un
  • 在 python 中的 for 循环中创建唯一名称列表

    我想在 for 循环内创建一系列具有唯一名称的列表 并使用索引创建列表名称 这就是我想做的 x 100 2 300 4 75 for i in x list i 我想创建空列表 例如 lst 100 lst 2 lst 300 有什么帮助吗
  • Linq to SQL - 如何在 InsertOnSubmit() 之后查找 IDENTITY 列的值

    我在用LINQ 到 SQL将简单数据插入到表中而无需stored procedure 该表有一个主键 ID 列 它被设置为IDENTITY两者中的列SQL Server并在我的DBML 首先我打电话InsertOnSubmit 包含单行数据
  • Shopify 元字段的限制

    我想知道 Shopify 中的实体可以拥有的元字段数量有哪些限制 例如 在产品对象的给定命名空间下 您可以拥有 1000 个唯一键值对吗 有硬性限制吗 请注意 我已经查阅了 Shopify 的 Metafield API 页面上的文档 ht
  • 阻止 href 打开链接,但仍执行其他绑定事件

    我想阻止链接打开页面 所以我写了这个 a click function e e preventDefault 这太棒了 但这阻止了我的其他活动 toolbar a click function e action 当然 我可以通过一些测试将我
  • 在 Java 中,final 字段可以从构造函数助手中初始化吗?

    我有一个最终的非静态成员 private final HashMap
  • 将整数转换为小数的高效算法

    CLRS 算法书的问题 31 1 12 提出了以下问题 给出一个有效的算法来转换给定的 bit 二进制 整数到十进制表示 论证如果对长度最多为 的整数进行乘法或除法 需要时间M 那么就可以及时进行二进制到十进制的转换 M lg Hint 使
  • 如何检查 Elixir 中的内存使用情况?

    Elixir 中可以检查内存使用情况吗 也许调用 Erlang 这就是我想要实现的目标 检查总内存使用情况 比方说1G of 8G Read a 4G file 检查总内存使用情况 比方说5G of 8G erlang memory tot
  • 如何确保文本框是字母数字但没有前导数字?

    我的 Web 应用程序包含一个文本框 我想限制其输入 我想阻止用户输入以下文本 以空白开头 以数字以外的内容开头 在前导字符之后包含字母数字字符 谢谢你的建议 不要以字母数字的空格开头 a zA Z 后跟 0 个或多个字母数字 a zA Z
  • Python中浮点数的分数[重复]

    这个问题在这里已经有答案了 当数字作为浮点数传递时 为什么分数的分子和分母更大 gt gt gt from fractions import Fraction gt gt gt gt gt gt a Fraction 2 34 gt gt
  • Javascript:识别是桌面Linux还是Android

    无法识别是linux桌面机还是android设备使用navigator userAgent or navigator platform因为某些 Android 设备有字符串linux同时 详情如下 Device OS navigator p
  • 启动 python 进行 Web 开发的好资源吗?

    我对学习 Python 进行 Web 开发非常感兴趣 有人能指出我正确的方向吗 我一直在 Google 上查看相关内容 但还没有真正找到任何显示正确文档以及如何开始使用的内容 有推荐的框架吗 教程 我从事 PHP 工作已有 5 年了 所以我
  • 获取复选按钮状态

    我如何获得 state of a Tkinter Checkbutton By state 我的意思是看看它是否有复选标记 当你创建它时 需要一个variable关键字参数 通过它IntVar from Tkinter 选中或取消选中该框将
  • 如何将 Kotlin 支持添加到您的 flutter 项目中?

    阅读后README文件的条形码扫描插件 https pub dev packages barcode scan我遇到了指令 这个插件是用 Kotlin 编写的 因此 需要添加Kotlin 支持您的项目 请参阅安装 Kotlin 插件 htt
  • 如何在Windows上正确使用CMAKE_MODULE_PATH

    修改此变量以便 CMake 可以找到项目指定为需求的适当模块的正确方法是什么 它似乎是自动生成的 我找不到可以以任何方式修改此路径的环境变量 我也很难找到能很好解释这一点的文档 只有 安装 CMake 包的说明 没有具体说明如何完成此操作的
  • ng-repeat 仅显示最后一个元素

    我正在使用 AngularJs 开发消息收件箱 我遇到了一个问题 我想仅显示其中的最后一个元素ng repeat 我做了一些研究 发现下面的代码应该可以工作 div class inbox ul div span recipient id
  • Flutter 将 Draggable Scrollbar 添加到 CustomScrollView?它一直给我“参数类型 CustomScrollView 无法分配给 BoxScrollView”

    一个简单的例子是 Scaffold floatingActionButton fab floatingActionButtonLocation fabLocation body Scrollbar child CustomScrollVie
  • 并发/待办事项示例不起作用

    我正在尝试 ToDo 示例 并在尝试并发处理时遇到了未处理的异常 dataservice js 包含这些行saveFailed error method if detail detail ExceptionType indexOf Opti
  • 该证书的颁发者无效 Apple Push Services

    我已创建证书以在我的应用程序中启用推送服务 但每次我尝试在钥匙串中添加证书时 添加证书后都会显示以下错误 该证书的颁发者无效 我想我已经弄清楚了这一点 我导入了新的 WWDR 证书将于 2023 年到期 https developer ap
  • Codeigniter 中国家/地区和城市的 Ajax 下拉菜单?

    我正在我们的 Codeigniter 框架中借助 ajax 使国家和城市下降 数据库的结构如下所示 Country country id country name State country id state id state name c