how to change a select list option when other select list changed?

混江龙づ霸主 提交于 2019-12-23 06:13:52

问题


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?

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

Thanks,

Shah RUkh


回答1:


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').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. 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>



回答2:


Okay I have done it, using 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



来源:https://stackoverflow.com/questions/14277361/how-to-change-a-select-list-option-when-other-select-list-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!