在ASP.NET MVC中使用父子组合生成下拉列表 [英] Generate dropdownlist with parent child combination in ASP.NET MVC

查看:68
本文介绍了在ASP.NET MVC中使用父子组合生成下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I need dropdownlist with parent child combination like below example

Parent 1
 Child of Parent 1
 Child of Parent 1 
Parent 2
 Child of Parent 2
 Child of Parent 2
 Child of Parent 2
 Child of Parent 2

I need to show option like above where both parent and child option can be selected by users.





我尝试过:



我已经检查了jquery示例但是父母无法选择。我需要同时选择父和子这两个选项。



[我们只能选择子项的演示]

推荐答案

检查这个



check this

<!DOCTYPE html>
<html>
<head>
    <title></title>    

</head>
<body>
    <select id="ddl"></select>

    <script>

        var json = [
            { parent: '1', item: 'Electronics' },
            { parent: '0', item: 'Play station' },
            { parent: '0', item: 'audios' },
            { parent: '1', item: 'Books' },
            { parent: '0', item: 'Non-Fiction' },
            { parent: '0', item: 'Maths' },
        ];
        options = [];
        var ddl = document.getElementById('ddl');
        for (var i = 0; i < json.length; i++) {

            var style = '', space = '&nbsp;&nbsp;'
            if (json[i].parent == '1')
            {
                style = "style='font-weight:bold'"
               space =''

            }
            var html = "<option " + style + "  > " + space + json[i].item; +" </option>"

            options.push(html);
        }

        ddl.innerHTML = options.join('\n'); 
    </script>
</body>
</html>





demo:编辑小提琴 - JSFiddle [ ^ ]


这篇关于在ASP.NET MVC中使用父子组合生成下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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