非常感谢您在以下问题上的帮助。我有3个动态相关下拉列表。方案1在以下情况下工作:单击下拉列表(dd)1=>dd2中的值更改=>dd3中的值更改。但我需要dd3也依赖于dd1和dd2。我有ajax代码,假设它有错误:
$(document).ready(function(){
$('#country', '#state').on('change',function(){
var countryID = $('#country').val();
var stateID = $('#state').val();
if(countryID, stateID){
$.ajax({
type:'POST',
url:'search_city.php',
data: "country_id="+countryID+"&state_id="+stateID,
success:function(html){
$('#city').html(html);
}
});
}else{
$('#city').html('<option value="">Select state first</option>');
}
})
});
添加php文件:
<?php
include('connect.php');
if (isset($_POST["country_id"] || isset($_POST["state_id"]))) {
$query = $conn->query("SELECT * FROM city WHERE country_id =
".$_POST['country_id']." AND state_id = ".$_POST['state_id']." ORDER BY
city_name");
$rowCount = $query->num_rows;
if($rowCount > 0){
echo '<option value="">Select city</option>';
while($row = $query->fetch_assoc()){
echo '<option
value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
}
}else{
echo '<option value="">City not available</option>';
}
}
?>
暂无答案!
目前还没有任何答案,快来回答吧!