mvc JQuery JSON.parse() 填充下拉列表

发布于 2024-09-29 15:03:07 字数 857 浏览 2 评论 0原文

我想填充从 JSON.parse() 返回的 Javascript 对象的下拉列表。尝试过但没有成功。请帮忙

<script type="text/javascript" charset="utf-8">
<br />
    //This is returned from Model. All I want to put that in dropdown.<br />
    //var cat = JSON.parse('[{"Selected":false,"Text":"Blue","Value":"1"},{"Selected":false,"Text":"Red","Value":"2"},{"Selected":false,"Text":"Grey","Value":"3"},{"Selected":false,"Text":"Black","Value":"4"},{"Selected":false,"Text":"Green","Value":"5"},{"Selected":false,"Text":"Olive","Value":"6"},{"Selected":false,"Text":"Orange","Value":"7"},{"Selected":false,"Text":"Pink","Value":"8"},{"Selected":false,"Text":"Violet","Value":"9"},{"Selected":false,"Text":"Yellow","Value":"10"}]');

<br /><br />
    var cat = JSON.parse('<%= Html.ToJson(Model.Colors) %>');
<br />
</script>

I want to fill dropdown from the Javascript object returned from JSON.parse(). Tried but not successfull. Please help

<script type="text/javascript" charset="utf-8">
<br />
    //This is returned from Model. All I want to put that in dropdown.<br />
    //var cat = JSON.parse('[{"Selected":false,"Text":"Blue","Value":"1"},{"Selected":false,"Text":"Red","Value":"2"},{"Selected":false,"Text":"Grey","Value":"3"},{"Selected":false,"Text":"Black","Value":"4"},{"Selected":false,"Text":"Green","Value":"5"},{"Selected":false,"Text":"Olive","Value":"6"},{"Selected":false,"Text":"Orange","Value":"7"},{"Selected":false,"Text":"Pink","Value":"8"},{"Selected":false,"Text":"Violet","Value":"9"},{"Selected":false,"Text":"Yellow","Value":"10"}]');

<br /><br />
    var cat = JSON.parse('<%= Html.ToJson(Model.Colors) %>');
<br />
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烛影斜 2024-10-06 15:03:07

您可以使用以下代码来完成此操作,

HTML 是,

<input type="button" id="btnclick" value="add" />

<select id="drpTemp">
</select>

JQuery 代码是,

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $("#btnclick").click(function () {            
            //This is returned from Model. All I want to put that in dropdown.<br />
            var cat = JSON.parse('[{"Selected":false,"Text":"Blue","Value":"1"},{"Selected":false,"Text":"Red","Value":"2"},{"Selected":false,"Text":"Grey","Value":"3"},{"Selected":false,"Text":"Black","Value":"4"},{"Selected":false,"Text":"Green","Value":"5"},{"Selected":false,"Text":"Olive","Value":"6"},{"Selected":false,"Text":"Orange","Value":"7"},{"Selected":false,"Text":"Pink","Value":"8"},{"Selected":true,"Text":"Violet","Value":"9"},{"Selected":false,"Text":"Yellow","Value":"10"}]');            
            var items = cat;
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                var optionhtml = '<option value="' + item.Value + '>' + item.Text + '</option>';
                $("#drpTemp").append(optionhtml);
            }
        });
    });
</script>

它将在单击按钮时填充下拉列表。

You can do this by using following code,

HTML is,

<input type="button" id="btnclick" value="add" />

<select id="drpTemp">
</select>

JQuery Code is,

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $("#btnclick").click(function () {            
            //This is returned from Model. All I want to put that in dropdown.<br />
            var cat = JSON.parse('[{"Selected":false,"Text":"Blue","Value":"1"},{"Selected":false,"Text":"Red","Value":"2"},{"Selected":false,"Text":"Grey","Value":"3"},{"Selected":false,"Text":"Black","Value":"4"},{"Selected":false,"Text":"Green","Value":"5"},{"Selected":false,"Text":"Olive","Value":"6"},{"Selected":false,"Text":"Orange","Value":"7"},{"Selected":false,"Text":"Pink","Value":"8"},{"Selected":true,"Text":"Violet","Value":"9"},{"Selected":false,"Text":"Yellow","Value":"10"}]');            
            var items = cat;
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                var optionhtml = '<option value="' + item.Value + '>' + item.Text + '</option>';
                $("#drpTemp").append(optionhtml);
            }
        });
    });
</script>

It will Fill Dropdownlist on Button Click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文