一个javascript+json 实现级联查询

主要参考了sugarcrm中的部分代码,json由后台代码生成


html:

<select id="select1"    
οnchange="addVariables(document.getElementById('select2'),this.options[this.selectedIndex].value);">
</select>
<select id="select2"></select>

javascript:

function addVariables(the_select, the_module) {
    the_select.options.length = 0;
        for (var i = 0; i < field_defs[the_module].length; i++) {
            var new_option = document.createElement('option');
            new_option.value = field_defs[the_module][i].name;
            new_option.text = field_defs[the_module][i].value;
            the_select.options.add(new_option, i);
        }
}
var selectabc = document.getElementById('select1');
selectabc.options.length = 0;
count = 0;
for (var i in field_defs) {
var optionNew = document.createElement('option');
        optionNew.value = i;
        optionNew.text = i;
        //alert(i + count);
        selectabc.options.add(optionNew, count++);
}
addVariables(document.getElementById('select2'), 'Contacts');


json(已经转换成javascript数组):

var field_defs = {
    "Contacts": [{
        "name": "a",
        "value": "a"},
    {
        "name": "b",
        "value": "b"}],
    "Users": [{
        "name": "c",
        "value": "c"},
    {
        "name": "d",
        "value": "d"}]
};


例子: http://download.csdn.net/detail/pipi7772/4234264


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值