簡體   English   中英

與ajax和json的依賴下拉列表

[英]dependent dropdown with ajax and json

我試圖在php中創建相關的下拉字段。 一切進展順利,即使它顯示了已打印的數組值,但json返回的值也不會打印在第二個下拉列表上。 你能建議我該怎么辦?

        <?php
        $id=$_GET["id"];
        $stmt = $conn->prepare("SELECT  Id, Title FROM category where DestId='$id' ");
        //$stmt->bindValue(1,$_GET["id"]);

        // Fetch the foods using the group id
        if ($stmt->execute()) {
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
            echo "<pre>".print_r($result)."</pre>";


            // If there are results, json encode them as the reply.
            if ($result) {
                echo json_encode($result);
            }
        }


?>

<div class="tripsearch">
  <h2>Rechercher un voyage !</h2>
  <form id="form1" name="form1" action="<?php echo URL_PATH.'search.php';?>" method="get">
            <select name="destination" id="destination" class="inputBox">
                  <option value="" selected="selected">Destinations</option>
                  <?php $dest_stmt = $conn->prepare("SELECT Id, Title FROM destinations");
                  $dest_stmt->execute();
                  while ($dest_rows = $dest_stmt->fetch()){
                      echo "<option value=\"".$dest_rows['Id']."\">".$dest_rows['Title']."</option>";
                  }
                  ?>                  
            </select>
       <select name="category" id="category"  class="inputBox">
                  <option value="any" selected="selected">Nos activit&eacute;s</option>
      </select>
    <input type="submit" id="search-btn" class="btn btn-org" value="OK" />
  </form>
</div>


<script language="javascript">
    $("#destination").change(function() {
        var Id = $(this).val();
        //alert(Id);

        $.ajax({
            type: "GET",
            //url: "http://192.168.1.120/current-projects/odysseenepaltrekking.com/include/search.php",
            data: { id: Id },
            dataType: "json"
        }).done(function(result) {
            // Clear options
            //$("#category").find("option").remove();
            // Loop through JSON response
            $.each(result, function(key, value) {   
                $('#category').append($('<option>', { value: value.Id }).text(value.Title));

            });
        });

    });
</script>

嘗試

$('#category').append('<option value='+value.Id+'>'+value.Title+'</option>');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM