选中后,下拉选项会更改

时间:2013-01-20 13:36:01

标签: php javascript

我正在尝试创建一个2下拉列表。选择第一个下拉列表时,第二个下拉列表会更改值。我似乎无法在第二个下拉列表中插入名称。这是我的代码

<script type='text/javascript'>
$(window).load(function(){
$("#category").change(function () {
    if ($(this).data('options') === undefined) {
        /*Taking an array of all options-2 and kind of embedding it on the select1*/
        $(this).data('options', $('#select2 option').clone());
    }
    var id = $(this).val();
    var options = $(this).data('options').filter('[value=' + id + ']');
    $('#select2').html(options);
    //alert(options);
});
});

</script>

<form action = '' method = 'POST'>
  <select name="select1" id="category">
        <option><--Destinations--></option>
        <?php
            $destination = mysql_query("SELECT * FROM destination");
            while ($row = mysql_fetch_assoc($destination))
            {
                $destid = $row['destination_id'];
                $destname = $row['destination_name'];
                echo"<option value=".$destname.">".$destname."</option>";
            }

        ?>
</select>

<select name="items" id="select2">
    <option><--Hotels--</option>
    <?php
        $hotel = mysql_query("SELECT * FROM hotel");
        while ($row = mysql_fetch_array($hotel))
        {
            $hotel_location = $row['hotel_location'];
            $hotel_name = $row['hotel_name'];
            echo"<option value=".$hotel_location.">".$hotel_name."</option>";
        }
    ?>
</select>
<input type = "submit" name="add"> 
</form> 



<?php
if(isset($_POST['select1'],$_POST['items']))
{
    $destination_name = $_POST['select1'];
    $hotel_name = $_POST['items'];
    echo $destination_name;
    //echo $destination_id;

}
?>

它已经输出了酒店位置,但我似乎找不到获取所选酒店名称的方法。请帮忙

1 个答案:

答案 0 :(得分:0)

   <script type='text/javascript'>
   $(window).load(function(){
   $("#category").change(function () {
       if ($(this).data('options') === undefined) {
       /*Taking an array of all options-2 and kind of embedding it on the select1*/
        $(this).data('options', $('#select2 option').clone());
   }
   var id = $(this).val();
   var options = $(this).data('options').filter('[value=' + id + ']');
   $('#select2').html(options);
   //alert(options);
   });
   });

   </script>

   <form action = '' method = 'POST'>
   <select name="select1" id="category">
    <option><--Destinations--></option>
    <?php
        $sql = "SELECT * FROM destination";
        $destination =  = $conn->Execute($sql);
        while ($row = $destination)
        {
            $destid = $row['destination_id'];
            $destname = $row['destination_name'];
            echo"<option value=".$destname.">".$destname."</option>";
        }

    ?>
   </select>

 <select name="items" id="select2">
 <option><--Hotels--</option>
 <?php
    $sql = "SELECT * FROM hotel";
    $hotel = $conn->Execute($sql);
    while ($row = $hotel)
    {
        $hotel_location = $row['hotel_location'];
        $hotel_name = $row['hotel_name'];
        echo"<option value=".$hotel_location.">".$hotel_name."</option>";
    }
?>

         

相关问题