简体   繁体   English

选择选项标签中的值时如何从json数组中获取更多数据

[英]How to get more data from json array when a value in option tag is selected jquery

I'm trying to display more info of the museum when a museum name is clicked fro the option dropdown box. 当尝试通过选项下拉框单击博物馆名称时,我试图显示有关该博物馆的更多信息。 I am getting my data from a json array. 我正在从json数组中获取数据。 I was able to populate the option box with the name of the museum but could not display more details. 我能够用博物馆的名称填充选项框,但无法显示更多详细信息。 Could anyone please help me with this. 谁能帮我这个忙。 I'm using jquery for this code. 我正在为此代码使用jquery。

function getMuseums() {

    var museumSelect = $("<select id=\"museumlist\" name=\"museumlist\" onChange=\"getMuseumInfo()\" />");
    var info = $("<span>Select Museum: </span>")

    $.get("museums.php",function(data,status) {
        var response = '';
        var json = $.parseJSON(data);
        museums = json.museums;
        $.each(museums, function (index, item) {

            $('<option />').attr({"value" : index}).text(item.museum_name).appendTo(museumSelect);

            /*response += "<option value='"+index+"'>" + item.museum_name + "</option>";
            $("#museumlist").html(response);*/

        });

        $('#content').empty().append(info, museumSelect);
        $("<div id=\"museumDetails\" />").appendTo("#MuseumDiv");

        getMuseumInfo()

    });
}

function getMuseumInfo() {
    var museum_id = $("#museumlist").val();
    selectedMuseumid = museum_id;
    $("#MuseumDiv").empty().append("<div id=\"museumDetails\"/>");

    var url = "museums.php?museum_id=" +escape(museum_id);

    $.get(url,function(data,status) {
        var json = $.parseJSON(data);
        museums = json.museums;

        var museum_name = museums.museum_name;
        var museum_description = museums.museum_description;

        var museumInfo = "<h3>" + museum_name + "</h3><p>" + museum_description + "</p>";

        $("#museumDetails").empty().append(museumInfo);

    });
}

You can pass option value to your function as a parameter getMuseumInfo(this.value) : 您可以将选项值作为参数getMuseumInfo(this.value)传递给函数:

  var museumSelect = $("<select id=\"museumlist\" name=\"museumlist\" onChange=\"getMuseumInfo(this.value)\" />");

And than you can use iterate through museums and check if index is the same as option value, and then print only properties for that item. 然后,您可以遍历博物馆,检查索引是否与选项值相同,然后仅打印该项目的属性。

$.each(museums, function(index, item) {    
    if (index == id) {
      var museumInfo = "<h3>" + item.museum_name + "</h3><p>" + item.museum_description + "</p>";    
      $("#MuseumDiv").empty().append(museumInfo);
    }
});

But better check working code (also turn on console in app): codePen 但是最好检查工作代码(也可以在应用程序中打开控制台): codePen

NOTE: I make example of request to create JSON data. 注意:我以创建JSON数据的请求为例。 I make get request to url: https://api.myjson.com/bins/58j10 我向网址发出请求: https : //api.myjson.com/bins/58j10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JQuery / JSon从选择选项中获取选择的项目 - How to get selected item from select option using JQuery/JSon 如何使用jQuery数据表的onChange事件从html select标签选项值中获取值? - How to get value from html select tag option value using onChange event using jQuery data table? 如何在jquery中选择更改时获取所选选项的数据值 - How to get data-value of selected option on select change in jquery 渲染从jQuery Ajax中选择Tag,然后从选定的选项中获取值 - Render select Tag from jQuery Ajax , then get the value from selected option 如何获得选定的 <option>从表并存储到数组中? - How to get the selected value of <option> from table and store into array? 选择选项时如何获取数据信息的值 - how get the value of data info when selected option 如何使用来自json的数据填充href标记,并在单击时使用表单中的选定值进行发布 - How to fill href tag with data from json and when clicked use the selected value in form to post 如何使用Mustache.js从具有相等选项和值的数组中获取JSON子数组数据? - How to get JSON sub array data from array with equal option and value with Mustache.js? 如何在jquery中获取选定复选框的值并将其分配给json数组? - How to get the value of selected checkbox in jquery and assign it to a json array? 选择选项时,获取在选择标签中定义的数据值 - Fetching data value defined in the select tag when an option is selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM