创建表单,从包含值的对象中选择

发布于 2024-10-17 15:49:24 字数 454 浏览 2 评论 0原文

我有这样一个对象:

var options = {1: 'Running', 2: 'Falling', 3: 'Collapsing wall', 
        4: (...)};

我想使用从此对象中获取的 ooptions 创建表单的选择元素,所以类似的东西(我尝试过的代码不起作用,但你可以明白这个想法):

html = '<form action="" ><select name="block-action"><option>-------</option>';
for(k in obj){
    html += '<option value="'+k+'">'+obj[k]+'</option>'
}
html += '</select></form>'

I have such an object :

var options = {1: 'Running', 2: 'Falling', 3: 'Collapsing wall', 
        4: (...)};

I'd like to create form's select element with ooptions taken from this object, so something like that (code I tried which is not working, but you can get the idea) :

html = '<form action="" ><select name="block-action"><option>-------</option>';
for(k in obj){
    html += '<option value="'+k+'">'+obj[k]+'</option>'
}
html += '</select></form>'

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

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

发布评论

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

评论(2

花海 2024-10-24 15:49:24

我唯一能看到的是引用该对象的变量称为 options,但您使用的是变量名称 obj

在那次改变之后对我有用。

示例: http://jsfiddle.net/X66Su/1/

这个:

var options = {1: 'Running', 2: 'Falling', 3: 'Collapsing wall', 
    4: (...)};

应该是:

var obj = {1: 'Running', 2: 'Falling', 3: 'Collapsing wall', 
    4: (...)};

Only thing I can see is that your variable referencing the object is called options, but you're using the variable name obj instead.

Works for me after that one change.

Example: http://jsfiddle.net/X66Su/1/

this:

var options = {1: 'Running', 2: 'Falling', 3: 'Collapsing wall', 
    4: (...)};

should be:

var obj = {1: 'Running', 2: 'Falling', 3: 'Collapsing wall', 
    4: (...)};
坏尐絯℡ 2024-10-24 15:49:24
var html = []; // <-- make an array

//push the first bits of text onto it
html.push('<form action="" ><select name="block-action"><option>-------</option>');

//here's where we dynamically use the object. You were close! You used the wrong variable name, obj vs. options
for(k in options){
    html.push('<option value="'+k+'">',options[k],'</option>');
}

//and wrap up the html tags you opened to start
html.push('</select></form>');

//use html.join(''); to spit out the text

通常,字符串连接(将两个字符串添加在一起,就像在您的示例中一样)比将值推送到数组然后连接它们要慢得多。我建议你养成这个习惯。

var html = []; // <-- make an array

//push the first bits of text onto it
html.push('<form action="" ><select name="block-action"><option>-------</option>');

//here's where we dynamically use the object. You were close! You used the wrong variable name, obj vs. options
for(k in options){
    html.push('<option value="'+k+'">',options[k],'</option>');
}

//and wrap up the html tags you opened to start
html.push('</select></form>');

//use html.join(''); to spit out the text

Generally string concats (where you add two strings together, like in your example) are much slower than pushing values onto an array and then joining them. I'd suggest that habit for you.

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