douzhe1264 2014-08-08 12:27
浏览 54

选择框jquery

Can someone explain why this code shows me blank select box ? When I select first box which name is "firstbox" then it should be showing info in second box which id is "komandos" but it won't showing anything...

<script>

jQuery(function($) {
    $("#komandos").change(function() {
        var id = $("#id").val();
        if(isNaN(id)) {return;}

        $.ajax({
            type: "GET",
            url: "ajax.php",
            data: {'update_tm': '', 'id': id},
            dataType: 'json',
           success: function(data){

                $("#komandos").empty();

                for (var i = 0; i < data.length; i++) 
                {
                    $("#komandos").append('<option>'+data[i].team+'</option>');
                }
            }
        });
    });
});

</script>

ajax.php

<?php
include_once('inc/conn.php');
include_once('inc/futbolas.php');

if(isset($_GET['update_tm']) && is_numeric($_GET['id']))
{
$query = $pdo->prepare("SELECT `pirma_komanda`,`antra_komanda` FROM futbolas WHERE `id` = ?");
 $query->execute(array($_GET['id']));
if($query)
{
   $query = $query->fetch();
   $rez[0]['team'] = $query['pirma_komanda'];
   $rez[1]['team'] = $query['antra_komanda'];
   echo json_encode($rez);
}

}
  • 写回答

1条回答 默认 最新

  • doulianqi3870 2014-08-08 12:30
    关注

    you have to use JSON.parse like

    success: function(data){
    
                $("#komandos").empty();
                var result = JSON.parse(data);
                for (var i = 0; i < result.length; i++) 
                {
                    $("#komandos").append('<option>'+result[i].team+'</option>');
                }
            }
        });
    

    See JS FIDDLE

    评论

报告相同问题?