简体   繁体   English

当其他选择列表发生变化时,如何更改选择列表选项?

[英]how to change a select list option when other select list changed?

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? 我的html中有两个选择列表,两个都保存在数据库中,我想知道当用户使用jquery更改select 1时,如何更改selectlist 2中的值?

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 我试图在codeigniter中做这些事情,所以请帮助我,我之前使用jquery完成了这些事情,但是这是在简单的PHP中通过将值传递给jquery的加载函数,但这在codeigniter中不起作用,任何人都请帮我

Thanks, 谢谢,

Shah RUkh Shah RUkh

Say for example you have this drop down 比如说你有这个下拉菜单

   <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>

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下拉列表时,您应该获取值并向控制器函数执行AJAX请求,这将返回select_2的选项,而不是简单地将select_2的html替换为来自脚本的返回响应

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

For codeignitor use the base_url in the url parameter of ajax request. 对于codeignitor,请在ajax请求的url参数中使用base_url。 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. 您可以为base_url定义一个CDATA变量,您可以在ajax请求中使用该变量,您可以在header.php文件中定义它。

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

Okay I have done it, using jQuery 好吧,我已经使用jQuery完成了它

 <script type="text/javascript">
  $(document).ready(function()
    {
    $("#slect_1").change(function() 
        {
         id = $("#select_1").val();
         $("#select_2").load("<?php echo site_url('class/method'); ?>/"+id);
    });
      });
  </script>

Thanks, 谢谢,

Shah Rukh 沙鲁克

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

相关问题 选择其他选择选项时,使用变量值更新选择列表吗? - Update select list with variable values when other select option is selected? 当我选择另一个选择标记HTML的某个选项时,如何自动更改选择标记选项的值列表<select> - How to automatically change the value list of option of select tag when i select the certain option of another select tag HTML <select> 当值是动态的时,如何基于第一个选择列表选项更改第二个选择列表? - How to change a second select list based on the first select list option when values are dynamic? 如何基于选择其他选择选项来更改选择选项 - How to change a select option based on choosing other select option 从其他选择列表中选择任何帖子类型时,如何列出选择列表中的所有帖子(如果有) - How to list all posts (if any) in a select list when any post type selected from other select list 从选择列表中选择一个选项时如何运行php? - How to run php when an option is selected from a select list? 在HTML中选择选项更改时更改表单字段 - Change Form Field when select option changed in HTML PHP选择(选项列表) - Php select (Option list) 从Codeigniter的其他下拉框中选择一个选项时如何更改选择值 - how to change select values when select an option from other drop down box in codeigniter 如何使选择列表选项处于选中状态 - How to make a select list option selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM