通过jQuery读取数据

时间:2018-12-07 00:36:03

标签: javascript jquery

我尝试制作API。我只想通过此公开数据获得“标题”。 但它返回“未定义函数” 您可以帮我解决这个问题吗?

[https://datos.madrid.es/portal/site/egob/menuitem.ac61933d6ee3c31cae77ae7784f1a5a0/?vgnextoid=00149033f2201410VgnVCM100000171f5a0aRCRD&format=json&file=0&filename=201132-0-museos&mgmtid=118f2fdbecc63410VgnVCM1000000b205a0aRCRD&preview=full]

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>

<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>

<script>

    $(document).ready(function(){

 $.ajax({
        type:"GET",
        dataType : "json",
        url:"https://datos.madrid.es/portal/site/egob/menuitem.ac61933d6ee3c31cae77ae7784f1a5a0/?vgnextoid=00149033f2201410VgnVCM100000171f5a0aRCRD&format=json&file=0&filename=201132-0-museos&mgmtid=118f2fdbecc63410VgnVCM1000000b205a0aRCRD&preview=full",
        //crossOrigin: null


      success:function(data){


                var parseData = JSON.parse(JSON.stringify(data));
                console.log(parseData.graph);


                var CompanyArray = data.graph; 
                console.log(CompanyArray);

                var myData="";
                $.each(CompanyArray,function(key,value) {
                        myData += ('<option value='+value.title+'>' +'key:'+key+', Code:'+value.title+',Name:'+value.schedule + '</option>');
                });
                $("#tekbeCompnayList").html(myData);

        }
    });
var t_code = $('#tekbeCompnayList option:selected').attr('value');
document.write(t_code);
});


</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以使用Jquery的append函数添加HTML元素。

<select id="tekbeCompnayList"></select>

然后在您的jquery代码中编写以下代码段。

 $.each(CompanyArray,function(key,value) {
 $("#tekbeCompnayList").append('<option value='+value.title+'>' +'key:'+key+', Code:'+value.title+',Name:'+value.schedule + '</option>')
                });

然后,如果要获取所选选项,则需要在选择框更改时触发事件。

  $("#tekbeCompnayList").change(function(){
    var selectedVal = $(this).children("option:selected").val();
});