选择时,我有 3 个相互依赖的下拉框。例如,如果我从下拉 1 中选择一种类型,那么下拉 2 仅填充与我从第一个下拉框中选择的内容相关的选项。第三个下拉选项依赖于第二个下拉选项。
我面临的问题是,当我想选择不同的东西时,下拉框中的选项不会刷新。
谁能帮我?
这是我的表格:
<label>Tour Type </label>
<select id="tourtype" name="tourtype" required>
<option value="" selected="selected">--Select--</option>
<?php
$sql=mysql_query("Select tour_type_id,tour_name from tour_type");
while($row=mysql_fetch_array($sql))
{
$tour_type_id=$row['tour_type_id'];
$name=$row['tour_name'];
echo "<option value='$tour_type_id'>$name</option>";
}
?>
</select>
<label>Country</label>
<select id="country" name="country" class="country" required>
<option value="" selected="selected">-- Select --</option>
</select>
<label>Destination</label>
<select id="destination" name="destination" class="destination" required>
<option value="" selected="selected">-- Select --</option>
</select>
这是表单底部的js:
<script>
$('#tourtype').change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: "ajax.php",
data: {
id: id,
get_countries: 1
},
success: function (html) {
$("#country").empty();
$("#country").append(html);
},
error: function () {
alert("ajax failed");
}
});
});
$('#country').change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: "ajax.php",
data: {
id: id,
get_destination: 1
},
success: function (html) {
$("#destination").empty();
$("#destination").append(html);
},
error: function () {
alert("ajax failed");
}
});
});
</script>
最后这是 ajax.php
<?php
include('../config.php');
if ($_REQUEST['get_countries']) {
$sql = mysql_query("SELECT * FROM `countries` WHERE `tour_type_id`=" . $_REQUEST['id']);
$countries = "";
while ($row = mysql_fetch_array($sql)) {
$cid = $row['countries_id'];
$name = $row['countries_name'];
$countries .= "<option value='" . $cid . "'>" . $name . "</option>";
}
echo $countries;
} elseif ($_REQUEST['get_destination']) {
$destination = "";
$sql = mysql_query("SELECT * FROM `destination` where `country_id` =" . $_REQUEST['id']);
while ($row = mysql_fetch_array($sql)) {
$destination_id = $row['destination_id'];
$name = $row['destination_name'];
$destination .= "<option value='" . $destination_id . "'>" . $name . "</option>";
}
echo $destination;
}
?>