其他选择列表更改时,如何更改选择列表选项? [英] how to change a select list option when other select list changed?

查看:79
本文介绍了其他选择列表更改时,如何更改选择列表选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html中有两个选择列表,并且都将值保存在数据库中,我想知道当用户使用jquery更改选择1时如何更改选择列表2中的值吗?

I have two selectlists in my html and both have the values saved in database, I want to know how to change values in selectlist 2 when select 1 is changed by user using jquery?

我正在尝试在codeigniter中执行这些操作,所以请帮助我,我之前使用jquery进行过这些操作,但这是在简单的php中通过将值传递给jquery的load函数来完成的,但这在codeigniter中不起作用,请任何人帮助我

I am trying to do these things in codeigniter, so please help me, I have done these things using jquery before but that was in simple php by passing the values to the load function of jquery, but this is not working in codeigniter, anyone please help me

谢谢

Shah RUkh

Shah RUkh

推荐答案

例如,您有此下拉列表

   <select id="select_1">
       <option value="">Select</option>
       <option value="1">1</option>
       <option value="2">2</option>
   </select>

   <select id="select_2">
       <option value="">Select</option>
   </select>

在更改select_1下拉菜单时,您应该获取该值并向控制器函数执行AJAX请求,这将为您返回select_2的选项,而不是用脚本返回的响应替换select_2的html

On change of select_1 drop down you should get the value and do a AJAX request to your controller function which will return you the options for select_2 than simply replace the html of select_2 with the returned response from script

                $('#select_1').change(function(){
                    var id = $(this).val();
                    $.ajax({
                            type: "POST",
                            url: base_url+'controller/action/',
                            data: 'id='+id,
                            success: function(html){
                                $('#select_2').html(html);
                            }
                       });
                });

对于codeignitor,请使用ajax请求的url参数中的base_url.您可以为base_url定义一个CDATA变量,可以在ajax请求中使用它,也可以在header.php文件中对其进行定义.

For codeignitor use the base_url in the url parameter of ajax request. You can define a CDATA variable for base_url which you can use in your ajax requests, you can define it in your header.php file.

                <script type="text/javascript">
                   //<![CDATA[
                        base_url = '<?php echo base_url();?>';
                   //]]>
                </script>

这篇关于其他选择列表更改时,如何更改选择列表选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆