使用 .each 填充选择元素
我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在附加元素之前,您需要先构建元素。
当您调用
$('#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:
This way you build up the full select element including children before appending it.
您无法将半个标签附加到元素上,因此当您尝试将
"
首先创建选择元素:
现在您可以向其添加选项:
在循环之后将选择元素添加到单元格:
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:
Now you can add options to it:
After the loop you add the select element to the cell:
我认为构建 HTML 时使用字符串杂耍有点不优雅。考虑一下:
另外,我建议使用有意义的变量名称,而不是
k1
和v1
等。I think string juggling for building HTML is somewhat unelegant. Consider this:
Also, I recommend using variable names that have a meaning istead of
k1
andv1
and so on.$("#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
插入选项时,必须插入到 select 中,而不是插入到 #gaCell 中。我会在字符串中生成 HTML,然后调用
$("#gaCell").html(someHTML)
而不是多次 DOM 插入,这样会快一点;发布代码的提示,删除 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;A hint for postiong code, remove your console.logs, just adds noise.