1

我从一个列表框中获取值并将其传递给另一个列表框,我让所有东西都使用一个值 $Lid,但现在我需要两个 $Lid 和 $Cid,这是正确的方法吗?

    $(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>';
    }
}

?>

现在它没有返回任何东西,所以我不得不假设它是错误的。谢谢你。

4

4 回答 4

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 为您进行格式化。

于 2013-01-18T03:13:47.087 回答
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);
            }});});});
于 2013-01-19T03:56:19.657 回答
0

我不明白你为什么在两个变量中存储相同的值:

var LocationString = 'Lid=' + $(this).val();
var CityString = 'Cid=' + $(this).val();

可以简化为:

var LocationString = $(this).val();

然后你只有一个值,所以data应该是下面的格式

data: {
    'Lid': LocationString
}
于 2013-01-18T03:12:10.957 回答
0

数据应该是格式

data: {Lid : LocationString, Cid : CityString},

并检查您的查询结果是什么

检查它

print_r(mysql_fetch_array($sql))

如果您的查询没有任何结果,则在 while 循环内回显将不起作用

于 2013-01-18T03:24:13.813 回答