我想填充使用jquery和ajax的下拉列表。用jquery填充下拉列表
這是我的代碼的樣子。
<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時間我的網頁上。有什麼方法可以在時間.php中從列表中抓取單個選項並將它們添加到我的頁面上的下拉列表中?
在此先感謝。
在這裏看到:http://stackoverflow.com/questions/4814512/how-to-create-dropdown-list-dynamically-using-jquery –
[自動填充的jQuery](http://jqueryui.com/自動完成/)沒有用? –