javascript - 使用 jquery 填充下拉列表

标签 javascript php jquery

我正在尝试使用 jquery 和 ajax 填充下拉列表。

这是我的代码的样子。

<script>
           $(document).ready(function(){                               //This script uses jquery and ajax it is used to set the values in
                $("#day").change(function(){                // the time field whenever a day is selected.

                      var day=$("#day").val();
                      var doctor=$("#doctor").val();

                      $.ajax({
                          type:"post",
                          url:"time.php",
                          data:"day="+day+"&doctor="+doctor,
                          success:function(data){
                             $("#time").html(data);
                             }
                      });

                });
           });
   </script>

这是 time.php

   //some code for connecting to database

 $doctor = $_POST['doctor'];

 $day = $_POST['day'];

$query="SELECT * FROM schedule WHERE doctor='" .$doctor."'AND day='" .$day. "'";

$result = mysqli_query($con, $query);

echo" 
<select name='timing' id='timing'>";
$i = 0;                                 //Initialize the variable which passes over the array key values

$row = mysqli_fetch_assoc($result);    //Fetches an associative array of the row
$index = array_keys($row);             // Fetches an array of keys for the row.

    while($row[$index[$i]] != NULL)
    {

        if($row[$index[$i]] == 1) {             
            $res = $index[$i];
            echo jason_encode($res);

            echo "<option value='"  . $index[$i]."'>" . $index[$i] . "</option>";
    }
    $i++;
    }       

    echo "</select>";

  ?>

这只是将 time.php 上的列表放入我页面上的 div 时间中。有什么方法可以从 time .php 列表中抓取各个选项并将它们添加到我页面上的下拉列表中?

提前致谢。

最佳答案

尝试:

success:function(data)
{
var option = '';
$.each(data.d, function(index, value) {
    option += '<option>' + value.YourReturnParam + '</option>';
    });
 $('#yourDdlID').html(option);
}

关于javascript - 使用 jquery 填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18951679/

相关文章:

php - 为什么在 ubuntu 20 上安装 php7.4 我在控制台中有 php 8?

javascript - 隐藏 id 中包含变量的 <tr>

javascript - Ajax 响应仅工作一次 - 使用 PHP 的购物车

javascript - 如何从对象的数组中获取特定值?

javascript - 自动验证 htaccess (401) 用户名​​密码登录

php - 寻找一种在不返回的情况下调用函数时停止进程的方法

javascript - 获取每个动态创建的输入元素的动态值并将它们分配给单独的变量

javascript - 保留 html o/p 中的空格

javascript - 使用(纯)Javascript 加载 CSS 时的优缺点

javascript - 获取 Canvas 中控制台位置处的像素颜色