尝试使用AJAX动态填充第二个下拉列表时遇到问题

时间:2016-06-22 08:18:18

标签: javascript php jquery ajax

我正在尝试根据第一个下拉菜单的选定选项填充第二个下拉菜单但是我无法这样做我不知道我做错了什么但有些事情是错的我所有努力实现我想要的结果都冲到了地面因为它不工作,请帮助我 到目前为止,我已经做到了....

<?php
    $explode = explode("/", $_SERVER['REQUEST_URI']);
    $explode = explode("?", $explode[count($explode)-1]);

    echo $explode[0];

最后 get_data.php 文件

<select name="ddl_company" size="1"  class="form-control" id="ddl_company"  onchange="getId(this.value);">
  <option value="">Select Company</option>   
  <?php
  //Getting Company name from mysql and displaying it in the 1st dropdown having id ddl_company
  $query = mysql_query("select * from company where company_status='Active'order  by company_name asc");
  while ($r = mysql_fetch_array($query)) {
    if ($r['company_id'] == $ddl_company) {
      echo "<option selected value=$r[company_id]>$r[company_name]</option>" . "<BR>";
    } else {
      echo "<option value=$r[company_id]>$r[company_name]</option>";
    }
    // second drop down list which is going to fetch data from mysql db   based on the selected option of 1st dropdown
    ?>
    <select name="ddl_dept" size="1"  class="form-control" id="ddl_dept">    
      <option value=""></option>            
    </select>

    //ajax implementation of 2nd dropdown

    <script type="text/javascript">
      function getId(val)
      {
      $.ajax({
          type:"POST",
          url:"getdata.php",
          data:"company_id="+val,
          success:function(data)
          {
              $('#ddl_dept').html(data);
          }

      });
      }
    </script>

我已经完成了所有这一切,但它不起作用,请帮助我,我将非常感激。顺便说一句,这就是我的输出                                                                                      click this to show the result

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:jquery

$("select#ddl_dept").on('change',function(){
    var selected = $('#ddl_company option:selected').text();
   getId(selected );
});