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

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大