使用 2 个视图/文档用 JSON 数据填充下拉列表

发布于 2024-12-13 07:11:18 字数 1538 浏览 3 评论 0原文

我在使用一些 JSON 数据填充下拉列表时遇到一些问题,我怀疑发生错误的原因是我在 #stuff div 中附加 $.post 的方式,但我已经尝试了几种方法,但无法解决它的窍门。

选择 id="" 标签 & div 位于另一个视图中(它不是这个特定文档的一部分),以这种方式填充下拉列表是否有问题?

我试图提醒“listItems”,并且我已经得到了选项值等...不明白为什么它不会填充。

任何帮助将不胜感激。

来自 $.post =

{"childrendata":[{"id":"42","parent":"1","fName":"hej","lName":"nisse","birthdate":"2011-10-21"}]}

jQuery/js 的 Json 响应:

 $("#stuff").append(function(){


                $.post("show_relations", {},
                            function(data)
                            {
                            $("#stuff").empty();

                            json = eval('('+data+')');

                                if(data == '{"childrendata":[]}')
                                {
                                    $("#stuff").append("No relations registered.");
                                }
                                else
                                {
                                  var listItems= "";

                                      for (var i = 0; i < json.childrendata.length; i++)
                                      {
                                        listItems+= "<option value='" + json.childrendata[i].fName + "'>" + json.childrendata[i].lName + "</option>";
                                      }

                                    $("#child_list").html(listItems);
                                }
                            });
            });
    });

I'm having some trouble populating a dropdownlist with some JSON data, i suspect that the error occurs because of the way im appending the $.post within the #stuff div, but i've tried this a couple of ways and just wont get the hang of it.

The select id="" tag & the div lies within another view (it's not part of this particular document) , is that a problem for populating the dropdown-list this way?

Ive tried to alert out the "listItems" and i've got the option values etc... dont get it why it wont populate.

Any help would be appreciated.

Json-response from the $.post =

{"childrendata":[{"id":"42","parent":"1","fName":"hej","lName":"nisse","birthdate":"2011-10-21"}]}

The jQuery/js:

 $("#stuff").append(function(){


                $.post("show_relations", {},
                            function(data)
                            {
                            $("#stuff").empty();

                            json = eval('('+data+')');

                                if(data == '{"childrendata":[]}')
                                {
                                    $("#stuff").append("No relations registered.");
                                }
                                else
                                {
                                  var listItems= "";

                                      for (var i = 0; i < json.childrendata.length; i++)
                                      {
                                        listItems+= "<option value='" + json.childrendata[i].fName + "'>" + json.childrendata[i].lName + "</option>";
                                      }

                                    $("#child_list").html(listItems);
                                }
                            });
            });
    });

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

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

发布评论

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

评论(1

ぃ双果 2024-12-20 07:11:18

编辑:根据您的评论,我假设您的问题纯粹是单页的。

该代码的问题似乎是您尝试使用 .append()使用一个函数(这是有效的 jQuery),但该函数不会返回 jQuery 可以附加到“stuff”节点的任何内容; $.post 进行 Ajax 调用,该调用立即返回。

相反,请尝试类似以下内容(根据需要修改 Ajax 调用的 URL):

$.post("url/to/post/to", {},
    function(data) {
        $("#stuff").empty(); //Clear your stuff div

        var children = data.childrendata; //jQuery automatically unserializes json

        if(children.length == 0) {
            $("#stuff").append("No relations registered.");
        }
        else {
            $('#stuff').append('<select id="child_list"></select>');
            $.each(children, 
                function(index, value) {
                    //Append each option to the selectbox
                    $("#child_list").append("<option value='" + value.fName + "'>" + value.lName + "</option>");
                }
            );
        }
    },
    'json'
);

$.each() 是通用 jQuery 迭代器,它有助于整理代码。

其作用是向提供的 URL 发送 Ajax post,该 URL 使用序列化的 json 对象进行响应。回调获取该响应(jQuery 已自行反序列化),将新的选择添加到“#stuff”div,然后将动态创建的选项添加到新的选择。

尾注:很抱歉没有发布 $.each 文档的链接,StackOverflow 目前只允许我在一篇文章中发布 2 个超链接。

Edit: Based on your comment, I'll assume your problem is purely single-page.

The problem with that code would appear to be the fact that you're trying to use .append() with a function (which is valid jQuery), but that the function doesn't return anything that jQuery can append to the 'stuff' node; $.post makes an Ajax call, which returns immediately.

Instead, try something like the following (modifying the URL to the Ajax call as required):

$.post("url/to/post/to", {},
    function(data) {
        $("#stuff").empty(); //Clear your stuff div

        var children = data.childrendata; //jQuery automatically unserializes json

        if(children.length == 0) {
            $("#stuff").append("No relations registered.");
        }
        else {
            $('#stuff').append('<select id="child_list"></select>');
            $.each(children, 
                function(index, value) {
                    //Append each option to the selectbox
                    $("#child_list").append("<option value='" + value.fName + "'>" + value.lName + "</option>");
                }
            );
        }
    },
    'json'
);

$.each() is the generic jQuery iterator, which helps de-clutter the code.

What this does is make an Ajax post to the provided URL, which responds with the serialized json object. The callback takes that response (which jQuery has already unserialized by itself), adds a new select to the '#stuff' div, and then adds the dynamically-created options to the new select.

Endnote: My apologies for not posting the link to the $.each documentation, StackOverflow only allows me to post 2 hyperlinks in a single post currently.

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