[英]How to display a second dropdown menu if a certain value in the first dropdown is selected?
[英]Dropdown created via jquery ajax:how to get related items in second dropdown of selected value in first dropdown
我正在嘗試填充第二個下拉列表,但未填充過濾器數據,但它會提取所有州,而僅根據第一個下拉列表中選擇的國家/地區獲取相關州。請告訴我代碼中出了什么問題。找不到ID的值,因此所有州名都已填寫在下拉菜單中。
<script type="text/javascript" src="jquery-ajax/jquery.js"></script>
<script>
$(document).ready(function(){
$("#country").change(function(){
//var optionValue = $("select[name='country_select']").val();
var id=$("#country").val();
//var dataString = 'id='+ id;
//alert("datastring"+dataString);
$.ajax({
type: "POST",
url: "getstate.php",
data: {Country_Id:id},
beforeSend: function(){ $("#ajaxLoader").show(); },
complete: function(){ $("#ajaxLoader").hide(); },
success: function(response){
$("#stateAjax").html(response);
$("#stateAjax").show();
}
});
});
});
</script>
</head>
<?php
include("connection.php");
?>
<body>
<form method="post">
Select Country:<select name="country_select" id="country">
<option value="">Select Country</option>
<?php
$cntry = mysql_query("SELECT Country_Id, name FROM country ORDER BY name ASC");
while($row = mysql_fetch_array($cntry))
{
$id=$row['Country_Id'];
$name=$row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
?>
</select>
<span id="errmsg" style="display:none">There is no matching option</span>
<div id="ajaxLoader" style="display:none"><img src="jquery-ajax/ajax-loader.gif" alt="loading..."></div>
<div id="stateAjax" style="display:none">
<select name="state_select" id="state" style="display:none">
<option value="">Please Select</option></select></select>
</div>
</form>
</body>
getstate.php
<?php
echo $_POST['id'];
include("connection.php");
$cntry = mysql_query("SELECT * FROM state where country_id=".$_POST['id']);
?>
State:
<select name="state_select" id="state">
<option value="">Please Select</option>
<?php
while($row = mysql_fetch_array($cntry))
{
$id=$row['State_id'];
$name=$row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
?>
</select>
$.ajax({
type: "POST",
url: "getstate.php",
data: {country_id:id},
...
編輯1:也
var id=$('#country').val();
編輯2:getstate.php
include("connection.php");
$country_Id = intval($_POST['Country_Id']);//if country_Id is integer...if not:$country_Id = $_POST['Country_Id'];
$cntry = mysql_query("SELECT * FROM state where country_id=".$country_Id);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.