我从列表框中获取值并将其传递到另一个列表框,我使用了一个值$ Lid,但现在我需要两个$ Lid和$ Cid,这是正确的方法吗?< / p>
$(document).ready(function()
{
$(".Doggie").change(function()
{
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_city.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
});
$('.Kitty').live("change",function(){
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_area.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Pig").html(html);
}
});
});
});
</script>
</head>
<body>
<div id="frame1">
<label>Place :</label>
<select name="Doggie" class="Doggie" id="Doggie">
<option selected="selected">--Select Place--</option>
<?php
$sql = mysql_query("SELECT tblLocations.RestID as Lid, tblLocations.CityID as Cid, tblRestaurants.RestName as name
FROM tblRestaurants INNER JOIN tblLocations ON tblRestaurants.RestID = tblLocations.RestID
GROUP BY tblLocations.RestID, tblRestaurants.RestName
ORDER BY tblRestaurants.RestName ASC");
while($row=mysql_fetch_array($sql))
{
echo '<option value="'.$row['Lid'].''.$row['Cid'].'">'.$row['name'].'</option>';
} ?>
</select>
<label>City :</label>
<select name="Kitty" class="Kitty" id="Kitty">
<option selected="selected">--Select City--</option>
</select>
<label>Area: :</label>
<select name="Pig" class="Pig" id="Pig">
<option selected="selected">--Select Area--</option>
</select>
</div>
</body>
</html>
和...
<?php
require ('config.php');
if ($_POST['Lid']) {
$Lid = $_POST['Lid'];
$sql = mysql_query("SELECT tblLocations.RestId as Lid, tblLocations.CityID as Cid, tblCities.CityName as name
FROM tblLocations INNER JOIN tblCities ON tblLocations.CityID = tblCities.CityID
WHERE tblLocations.RestID = $Lid
GROUP BY tblLocations.RestID, tblCities.CityName
ORDER BY tblCities.CityName ASC");
echo '<option selected="selected">--Select City--</option>';
while ($row = mysql_fetch_array($sql)) {
echo '<option value="' . $row['Lid'] . '' . $row['Cid'] . '">' . $row['name'] . '</option>';
}
}
?>
现在它没有返回任何东西,所以我不得不承担错误。谢谢。
答案 0 :(得分:1)
我建议您进行以下更改:
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_city.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
您添加了两个data
值,这不是正确的方法。只需传递一个带有所需键和值的文字对象,并允许JQuery为您进行格式化。
答案 1 :(得分:0)
我不明白为什么你在两个vars中存储相同的值:
var LocationString = 'Lid=' + $(this).val();
var CityString = 'Cid=' + $(this).val();
可以简化为:
var LocationString = $(this).val();
然后您只有一个值,因此data
应为以下格式
data: {
'Lid': LocationString
}
答案 2 :(得分:0)
数据应为格式
data: {Lid : LocationString, Cid : CityString},
并检查您的查询结果
通过
检查print_r(mysql_fetch_array($sql))
如果你的查询没有任何结果,那么回路里面的循环将无法正常工作
答案 3 :(得分:0)
这样做了。
$(document).ready(function()
{
$(".Doggie").change(function()
{
var LocationString ='Rid='+ $(this).val();
$.ajax({
type: "POST",
url: "place_city.php",
data: LocationString,
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
});
$('.Kitty').live("change",function(){
var Rid = $('#Doggie').val(), // This is the value of the id="Doggie" selected option
Cid = $(this).val(); // This is the value of the id="Kitty" selected option
//alert("Rid = " + Rid + " Cid = " + Cid);
$.ajax({
type: "POST",
url: "place_area.php",
data: {"Rid":Rid,"Cid":Cid},
cache: false,
success: function (html) {
//alert('This is what is returned from the php script: ' + html);
$(".Pig").html(html);
}});});});