1
我試圖根據第一個選定的下拉選項填充第二個下拉列表。填充第二個<select>基於第一個下拉選項<select>下拉選項
這裏是我的工作至今:
form.php的 - 我用的JavaScript調用getgeneral.php。第二個下拉會顯示當用戶已經從(第一)下拉選擇:
<html>
<head>
<script type="text/javascript">
function get_states() { // Call to ajax function
var classitem = $('#classitem').val();
var dataString = "classitem="+classitem;
$.ajax({
type: "POST",
url: "getgeneral.php",
data: dataString,
success: function(html)
{
$("#get_general").html(html);
}
});
}
</script>
</head>
<body>
<form action="" method="POST">
<?php
include('conn.php');
$result=mysqli_query($con,"SELECT * FROM classtable");
?>
<select id="classitem" name="classitem" onchange="get_states();">
<option value=''>Select</option>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['genclasscode'] . "'>" . $row['description'] . "</option>";}
?>
</select>
<div id="get_general"></div>
</form>
</body>
</html>
這是getgeneral.php在那裏將獲取從第一個下拉列表基礎數據:
<?php
include('conn.php');
if ($_POST) {
$classitem = $_POST['classitem'];
if ($classitem != '') {
$result1=mysqli_query($con,"SELECT * FROM generalclass WHERE genclasscode='$classitem'");
echo "<select name='classitem'>";
echo "<option value=''>Select</option>";
while ($row = mysqli_fetch_array($result1)) {
echo "<option value='" . $row['specclassid'] . "'>" . $row['description'] . "</option>";}
echo "</select>";
}
else
{
echo '';
}
}
?>
問題:第二個下拉將不會出現。當我運行時沒有錯誤顯示form.php
您可以在更改第一個值時檢查您的網絡請求嗎?它有時會告訴你一個不會輸出的錯誤。打開頁面後點擊'F12',然後捕獲請求 – Chitowns24
Uncaught ReferenceError:$未定義 –
您忘記了包含jquery,或者您有衝突。看起來像前者,如果代碼是完整的 – Bryan