使用JSON填充下拉菜单 [英] Populate dropdown using json

查看:134
本文介绍了使用JSON填充下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何使用JSON对象填充下拉列表. 我使用php将json发送到客户端.客户端使用ajax捕获json数据. 我使用下面的代码来检查我到底从服务器收到了什么.

I need to know how to populate a dropdown using the JSON object.. Im using php to send json to the client.. client catches the json data using ajax. i use below code to check what i exactly receive from the server.

  var test = xmlhttp.responseText;
  alert(test);

结果显示给我..如下...

the result was showing me.. as below...

   {"2":"pricelist.xml","3":"camera.xml", "4":"cd.xml","5":"data.xml"}

我的确切做法是..我在服务器上设置了xml文件,使用php读取了这些文件名,然后使用json_encode将其转换为json对象,然后我将其发送给客户端,以使用json数据填充下拉列表我从服务器发送的.如我上面测试的,我在客户端接收json数据没有问题.我需要知道如何使用上述json数据填充数据

what i exactly do is.. i have set of xml files on the server im reading those filenames using php and i convert it to json object using json_encode and then i send that ot the client to populate the dropdownlist using the json data which i send from server. I have no issue in receiving the json data in client side as i tested above. i need to know how do i populate the data using above json data

推荐答案

您的HTML

<select id="myselect">
</select>

javascript(使用 jQuery )

The javascript(Using jQuery)

var json={} // Populate this json object
$.each(json, function(key, value){
    $('#myselect').append("<option value='"+key+"'>"+value+"</option>");
});

这篇关于使用JSON填充下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆