jQuery级联下拉问题

时间:2012-06-10 12:36:25

标签: php jquery html drop-down-menu

我已经在这里得到了一些帮助,但不幸的是我仍然在努力应对级联下拉菜单。我需要根据<select>的选择更新下面的#city,其上方是<select>框。

我的代码中是否有明显的错误?

HTML / jQuery的:

<select name="club" class="dropdown" id="club">
<script>

$('#city').change(function(){
    var $club = $('#club');
    $club.find('option:not([value="default"])').remove(); //Remove previous items
    $.getJSON('getClubs.php', {city:$(this).val()}, function(clubs){
        $.each(clubs, function(index, city){
            $club.append('<option value="'+row[0]+'">'+row[1]+'</option>');
        });
    });
});

</script>
</select>

PHP(getClubs.php):

<?php

date_default_timezone_set('Europe/London');

$day = date("l");
$time = date("G");

if ($time >= 21) {
    $day = date('l', strtotime($day .' +1 day'));
    }

$city = $_POST['city'];

if ($day == Monday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')";
    }

else if ($day == Tuesday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY')";
    }

else if ($day == Wednesday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY')";
    }

else if ($day == Thursday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY')";
    }

else if ($day == Friday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY')";
    }

else if ($day == Saturday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY')";
    }

else if ($day == Sunday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY')";
    }

    $result = mysql_query($query);
    $items = array();

    if($result && mysql_num_rows($result) > 0) { 
        while ($row = mysql_fetch_array($result)) { 
            $items[] = array( $row[0], $row[1] );
            }         
    } 

    mysql_close(); 
    // convert into JSON format and print
    echo json_encode($items);
?>

1 个答案:

答案 0 :(得分:0)

我认为这些都是您的代码问题:

  1.  Take look the html code?
      <select name="club" class="dropdown" id="club">
      </script>

     Question:
      Why you have to trigger this $("#city") neither to use  of $('.dropdown') or $('#club')  itself. Is that be posible to trigger the option ? I think its not .

     try:


           $('.dropdown').change(function(){
              var $club = $('#club');
             $club.find('option:not([value="default"])').remove(); //Remove previous items
                $.getJSON('getClubs.php', {city:$(this).val()}, function(clubs){
               $.each(clubs, function(index, city){
            $club.append('<option value="'+row[0]+'">'+row[1]+'</option>');
        });
     });
        });


  2. Where is the variable $row coming from?