json进入表单下拉列表 [英] json into form drop down list

查看:103
本文介绍了json进入表单下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Maps Places json填充下拉表单元素.

我尝试阅读许多页面,但无法弄清楚.

我相信这对于知道脚本的人来说很简单.我不.

如果可以的话请帮忙.
谢谢,
已删除@ gmail.com
[编辑-请不要在公共论坛中提供个人电子邮件地址.
每当有人发布答案时,您都会收到一封通知电子邮件.]

I am trying to use the google maps places json to populate a dropdown form element.

I have tried reading many pages but cannot figure it out.

I am sure this is something simple for someone who knows the scripting. I do not.

Please help if you can.
Thanks,
Removed@gmail.com
[Edited - Please do not provide personal email-address in public forum.
You will get a notification email whenever someone post an answer.]

推荐答案

尝试如下操作:

这是您的JSON对象.实际上,它将从您的服务器加载.

Try something like this:

This is your JSON object. Realistically, it would be loaded from your server.

<script type="text/javascript">
    var jsonObj = { location: [
        { "name": "france", "address": "merde" },
        { "name": "london", "address": "shiite" }
    ]
    }
</script>



在您的页面中,假设您有< select> id为"dropDown"的元素



In your page, assume you have a <select> element with an id of "dropDown"

<select id="dropDown"></select>



最后,当页面加载完成时,此代码将填充您的下拉列表.



Finally, when your page has finished loading, this code will populate your drop-down list.

var myDDL = document.getElementById("dropDown");

for (i = 0; i < jsonObj.location.length; i++) {
    var option = document.createElement("option");
    option.text = jsonObj.location[i].name;

    option.value = jsonObj.location[i].address;
    try {
        myDDL.options.add(option);
    }
    catch (e) {
        alert(e);
    }
}


这篇关于json进入表单下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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