我试图获取"州的列表"在我选择" country"。
之后国家/地区列表正确显示,但是当我选择国家/地区时,状态实际上并未显示在下拉选择表单中。
我使用ajax将数据从index.php发送到ajaxData.php,但我无法弄清楚我做错了什么。
任何帮助都将不胜感激。
这是index.php
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'marka_id='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#state').html('<option value="">Select country first</option>');
$('#city').html('<option value="">Select state first</option>');
}
});
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'modeli_id='+stateID,
success:function(html){
$('#state').html(html);
}
});
}else{
'');
}
});
});
</script>
<?php
//Include database configuration file
include('dbConfig.php');
//Get all country data
$query = $db->query("SELECT * FROM marka WHERE `status` = 1 ORDER BY marka_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="country" id="country">
<option value="">SelectMarken </option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['marka_id'].'">'.$row['marka_name'].'</option>';
}
}else{
echo '<option value="">Country doesnt exist</option>';
}
?>
</select>
<select name="state" id="state">
<option value="">Select state</option>
</select>
这是:ajaxData.php
<?php
//Include database configuration file
include('dbConfig.php');
if(isset($_POST["marka_id"]) && !empty($_POST["marka_id"])){
//Get all state data
$query = $db->query("SELECT * FROM modeli WHERE `marka_id` = ".$_POST['marka_id']." AND `status` = 1 ORDER BY modeli_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
//Display states list
if($rowCount > 0){
echo '<option value="">Select modelin</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['modeli_id'].'">'.$row['modeli_name'].'</option>';
}
}else{
echo '<option value="">Modeli doesnt exist</option>';
}
}
?>
[更新代码] 我忘了立即发布,所以现在这就是我的工作。
ajaxData.php
<?php
//Include database configuration file
include('dbConfig.php');
if(isset($_POST["marka_id"]) && !empty($_POST["marka_id"])){
//Get all state data
$query = $db->query("SELECT * FROM modeli WHERE `marka_id` = ".$_POST['marka_id']." AND `status` = 1 ORDER BY modeli_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
//Display states list
if($rowCount > 0){
$optionHTML = '<option value="">Select modelin</option>';
while($row = $query->fetch_assoc()){
$optionHTML .= '<option value="'.$row['modeli_id'].'">'.$row['modeli_name'].'</option>';
}
echo $optionHTML;
}else{
echo '<option value="">Modeli doesnt exist</option>';
}
}
?>
的index.php
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'marka_id='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#state').html('<option value="">Select country first</option>');
}
});
});
</script>
<?php
//Include database configuration file
include('dbConfig.php');
//Get all country data
$query = $db->query("SELECT * FROM marka WHERE `status` = 1 ORDER BY marka_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="country" id="country">
<option value="">SelectMarken </option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['marka_id'].'">'.$row['marka_name'].'</option>';
}
}else{
echo '<option value="">Ss</option>';
}
?>
</select>
<select name="state" id="state">
<option value="">Select state</option>
</select>
答案 0 :(得分:0)
错误发生在#state函数,我有另一部分代码,一旦状态被选中,就会等待城市的数据。
所以我不得不让'else'没有任何工作。
谢谢你们。