使用 .each 填充选择元素

发布于 2024-11-19 12:46:49 字数 1226 浏览 0 评论 0原文

我从 API 中返回了一个巨大的疯狂 JSON,并且我正在努力将其放入选择框内。到目前为止,我有这段代码:

$('#gaua').live('change', function () {

        var dropDownValue = $(this).val();

        $("#gaCell").html("<select>");

        $.each(gaAccount, function(k, v) {
            $.each(v, function(k1, v1) {
                console.log("k1 "+k1+" "+dropDownValue);
                if(k1 == dropDownValue) {
                    console.log("k1 is equal to dropDownValue");
                    $.each(v1, function(k2, v2) {
                        console.log("v1 "+v1+" k2 "+k2+" v2 "+v2);
                        $("#gaCell").append("<option value='"+v2+"'>"+k2+"</option>");
                    });
                }
            });
        });

        $("#gaCell").append("</select>");

        console.log($("#gaCell").html());

    });

当我查看控制台时,我看到了这样的内容:

<select></select><option value="4434">Option 1</option><option value="43333380">Option 2</option><option value="3233223">Option 3</option> ...

为什么最初会附加 ?是否应该设置开始标记,然后添加选项值,最后设置闭合选择标记?

代码看起来很糟糕,但在清理之前我需要让它正常工作。任何建议都会非常有帮助,谢谢!

I am getting back a huge crazy JSON from an API, and I am struggling with putting it inside a select box. So far I have this code:

$('#gaua').live('change', function () {

        var dropDownValue = $(this).val();

        $("#gaCell").html("<select>");

        $.each(gaAccount, function(k, v) {
            $.each(v, function(k1, v1) {
                console.log("k1 "+k1+" "+dropDownValue);
                if(k1 == dropDownValue) {
                    console.log("k1 is equal to dropDownValue");
                    $.each(v1, function(k2, v2) {
                        console.log("v1 "+v1+" k2 "+k2+" v2 "+v2);
                        $("#gaCell").append("<option value='"+v2+"'>"+k2+"</option>");
                    });
                }
            });
        });

        $("#gaCell").append("</select>");

        console.log($("#gaCell").html());

    });

When I look at the console I see this:

<select></select><option value="4434">Option 1</option><option value="43333380">Option 2</option><option value="3233223">Option 3</option> ...

Why is the <select></select> being appended initially? Should the opening tag be set, then the option values added and finally the closed select tag?

The code looks crap, but I need to get this working properly before I clean it up. Any advice would really help, thanks!

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

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

发布评论

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

评论(5

熟人话多 2024-11-26 12:46:49

在附加元素之前,您需要先构建元素。

当您调用 $('#gaCell').html('

试试这个:

    $('#gaua').live('change', function () {

        var dropDownValue = $(this).val();

        $select = $("<select>");

        $.each(gaAccount, function(k, v) {
            $.each(v, function(k1, v1) {
                console.log("k1 "+k1+" "+dropDownValue);
                if(k1 == dropDownValue) {
                    console.log("k1 is equal to dropDownValue");
                    $.each(v1, function(k2, v2) {
                        console.log("v1 "+v1+" k2 "+k2+" v2 "+v2);
                        $select.append("<option value='"+v2+"'>"+k2+"</option>");
                    });
                }
            });
        });

        $("#gaCell").append($select);

        console.log($("#gaCell").html());

    });

通过这种方式,您可以在附加元素之前构建包括子元素在内的完整选择元素。

You need to construct your element first, before appending it.

When you call $('#gaCell').html('<select>'), you're telling jQuery to append a select element, including closing tag.

Try this:

    $('#gaua').live('change', function () {

        var dropDownValue = $(this).val();

        $select = $("<select>");

        $.each(gaAccount, function(k, v) {
            $.each(v, function(k1, v1) {
                console.log("k1 "+k1+" "+dropDownValue);
                if(k1 == dropDownValue) {
                    console.log("k1 is equal to dropDownValue");
                    $.each(v1, function(k2, v2) {
                        console.log("v1 "+v1+" k2 "+k2+" v2 "+v2);
                        $select.append("<option value='"+v2+"'>"+k2+"</option>");
                    });
                }
            });
        });

        $("#gaCell").append($select);

        console.log($("#gaCell").html());

    });

This way you build up the full select element including children before appending it.

终陌 2024-11-26 12:46:49

您无法将半个标签附加到元素上,因此当您尝试将 "

首先创建选择元素:

var sel = $('<select/>');

现在您可以向其添加选项:

sel.append($('<option/>', { value: v2 }).text(k2));

在循环之后将选择元素添加到单元格:

$('#gaCell').append(sel);

You can't append half a tag to an element, so when you try to put "<select>" in the cell, the browser will make a complete select element out of it.

Create the select element first:

var sel = $('<select/>');

Now you can add options to it:

sel.append($('<option/>', { value: v2 }).text(k2));

After the loop you add the select element to the cell:

$('#gaCell').append(sel);
忆悲凉 2024-11-26 12:46:49

我认为构建 HTML 时使用字符串杂耍有点不优雅。考虑一下:

$('#gaua').live('change', function () {
    var dropDownValue = $(this).val();
    var $select = $("<select>").appendTo("#gaCell");

    $.each(gaAccount, function(i, acc) {
        $.each(acc, function(name, details) {
            if(name == dropDownValue) {
                $.each(details, function(label, value) {
                    $select.append("<option>", {value: value, text: label});
                });
            }
        });
    });
});

另外,我建议使用有意义的变量名称,而不是 k1v1 等。

I think string juggling for building HTML is somewhat unelegant. Consider this:

$('#gaua').live('change', function () {
    var dropDownValue = $(this).val();
    var $select = $("<select>").appendTo("#gaCell");

    $.each(gaAccount, function(i, acc) {
        $.each(acc, function(name, details) {
            if(name == dropDownValue) {
                $.each(details, function(label, value) {
                    $select.append("<option>", {value: value, text: label});
                });
            }
        });
    });
});

Also, I recommend using variable names that have a meaning istead of k1 and v1 and so on.

指尖凝香 2024-11-26 12:46:49

$("#gaCell").html("< /选择>';

$("#gaCell").append(""); 会将 '' 添加到末尾#gaCell 内的其他内容

$("#gaCell").html("<select>"); will make the contents of #gaCell = '<select></select>';

$("#gaCell").append("</select>"); will add '<select></select>' to the end of whatever else is inside #gaCell

孤蝉 2024-11-26 12:46:49

插入选项时,必须插入到 select 中,而不是插入到 #gaCell 中。我会在字符串中生成 HTML,然后调用 $("#gaCell").html(someHTML) 而不是多次 DOM 插入,这样会快一点;

   var html = "<select>";

    $.each(gaAccount, function(k, v) {
        $.each(v, function(k1, v1) {
            if(k1 == dropDownValue) {
                $.each(v1, function(k2, v2) {
                    html += "<option value='"+v2+"'>"+k2+"</option>";
                });
            }
        });
    });        
    html += "</select>";
    $("#gaCell").html(html);

发布代码的提示,删除 console.logs,只会增加噪音。

When you insert the options, you have to insert into the select, not into #gaCell. I would generate the HTML in a string and then call $("#gaCell").html(someHTML) instead of multiple DOM insertions, it's a little faster;

   var html = "<select>";

    $.each(gaAccount, function(k, v) {
        $.each(v, function(k1, v1) {
            if(k1 == dropDownValue) {
                $.each(v1, function(k2, v2) {
                    html += "<option value='"+v2+"'>"+k2+"</option>";
                });
            }
        });
    });        
    html += "</select>";
    $("#gaCell").html(html);

A hint for postiong code, remove your console.logs, just adds noise.

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