简体   繁体   English

如何从json数组获取值填充选区(下拉列表)

[英]How to get values from json array fill a Selection (drop down)

I am new to jquery and I am currently building an application in phonegap... 我是jquery新手,目前正在phonegap中构建应用程序...

I need to get a list of values displayed into the dropdown.. 我需要在下拉列表中显示一个值列表。

 "data": [
    {
      "city_id": "1",
      "city": "test1"
    },
    {
      "city_id": "2",
      "city": "test2"
    },
]

I get my data like this... I want to fill these values in the drop down which is found in a form. 我得到这样的数据...我想将这些值填写在表格中的下拉列表中。 When the user selects the city in the form I want the form to post city_id when it is posting the form data.... 当用户在表单中选择城市时,我希望表单在发布表单数据时发布city_id。...

Any kind of help is appreciated.... 任何帮助都将受到赞赏。

Its fairly straightforward with jQuery jQuery非常简单

Html HTML

<select></select>

JS JS

var d = '"data": [{"city_id": "1","city": "test1"},{"city_id": "2","city": "test2"}]';
var d = JSON.parse('{'+d+'}');

$.each(d.data, function(index, value){
  $('select').append('<option value="'+value.city_id+'">'+value.city+'</option>')
});

And there you have it! 在那里,您拥有了!

Here is a JSfiddle 这是一个JSfiddle

You can dynamically load each of these options into a <select> html element via jquery. 您可以通过jquery将每个这些选项动态地加载到<select> html元素中。

Each of these options would display the city as text, and city_id as value. 每个选项都将城市显示为文本,将city_id显示为值。 JS has a way of parsing JSON into a JS object by the function JSON.parse() . JS通过函数JSON.parse()将JSON解析为JS对象。

Here's a codeblock you can reference: 这是您可以参考的代码块:

var data = JSON.parse(*your data above*); // This is now an array
var $menu = $('#menu'); // this is in reference to the <select> menu you have in your HTML that you are appending to
for (var i = 0; i < data.length; i++) {
    var city = data[i];
    $menu.append('<option value='+city.city_id+'>'+city.city+'</option>');
}

This will populate your menu based on the JSON, and you can wrap the whole thing in a element so that it will pass the currently selected option on form submit. 这将基于JSON填充菜单,您可以将整个内容包装在一个元素中,以便它将通过表单提交中当前选择的选项。

You can show your city as text, city_id you can store as attr "city_id" in your tag: 您可以将城市显示为文本,将city_id显示为标签中的attr“ city_id”:

<div city_id="(city_id)">(city)</div>

and show it on click with jquery. 并在使用jquery单击时显示。

$("div[city_id]").click(function(){
alert($(this).attr("city_id"));
})

Hear some example : https://jsfiddle.net/RomanGroovyDev/hh2ydecb/ 听到一些例子: https : //jsfiddle.net/RomanGroovyDev/hh2ydecb/

You can try this: 您可以尝试以下方法:

HTML HTML

<select></select>

Data 数据

var dataObj =  {"data": [
    {
      "city_id": "1",
      "city": "test1"
    },
    {
      "city_id": "2",
      "city": "test2"
    },
]}

Script 脚本

Using forEach: 使用forEach:

dataObj.data.forEach(function (obj) {        
   $("<option />", {value: obj.city_id, text: obj.city}).appendTo($('select'));
})

Using for in: 用于:

for(i in dataObj.data) {
    $("<option />", {value: dataObj.data[i].city_id, text: dataObj.data[i].city}).appendTo($('select'));
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM