How we can bind select tag with variable data?..
我们如何将select标签与可变数据绑定?
Here is my sample code here.
这是我的示例代码。
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script>
var google.language.Languages = {
'AFRIKAANS' : 'af',
'ALBANIAN' : 'sq',
'AMHARIC' : 'am',
'ARABIC' : 'ar',
'ARMENIAN' : 'hy',
'AZERBAIJANI' : 'az' ...
}
</script>
I want Bind my select tag using above languages
我想使用上述语言绑定我的选择标记
<select id="langsel" name="selector">
<option value="">Select a language</option>
</select>
How to bind "langsel" select with above variable.?
如何用上面的变量绑定“langsel”select。?
I need output like below :
我需要如下输出:
<select id="langsel" name="selector">
<option value="">Select a language</option>
<option value="af">AFRIKAANS</option>
....
</select>
6 个解决方案
#1
9
$(function() {
var languages = {
'AFRIKAANS' : 'af',
'ALBANIAN' : 'sq',
'AMHARIC' : 'am',
'ARABIC' : 'ar',
'ARMENIAN' : 'hy',
'AZERBAIJANI' : 'az'
}
var sel = $('#langsel');
$.each(languages, function(key, value) {
sel.append('<option value="'+value+'">'+key+'</option>');
});
});
Working Fiddle: http://jsfiddle.net/qtb6S/
工作小提琴:http://jsfiddle.net/qtb6S/
#2
2
The answer is here: What is the best way to add options to a select from an array with jQuery?
答案在这里:使用jQuery从数组中向select中添加选项的最佳方法是什么?
Loop through your languages in a for loop, and use the code in that answer above.
在for循环中循环访问您的语言,并使用上面答案中的代码。
#3
1
var languages = [
'AFRIKAANS : af',
'ALBANIAN : sq',
'AMHARIC : am',
]
$.each(languages,function(index,elem){
$("#langsel").append(
$("<option/>").val(elem.split(':')[1]).text(elem.split(':')[0])
);
});
fiddle http://jsfiddle.net/3dFgg/
小提琴http://jsfiddle.net/3dFgg/
#4
0
The quickest (to type) way to do it would be to first build the HTML string, and then insert it into the <select>
:
最快(要键入)的方法是首先构建HTML字符串,然后将其插入
var languages = { /* ... */ },
str = '',
hop = Object.prototype.hasOwnProperty,
x;
for (x in languages) {
if (hop.call(languages, x)) {
str += '<option value="' + languages[x] + '">' + x + '</option>';
}
}
document.getElementById('langsel').innerHTML += str;
#5
0
You could do something like this:
你可以这样做:
var sel = $("#langsel");
var html = "";
var items = Languages;
html = $("<option></option>").text("Select a Language").val("");
sel.append(html);
html = "";
for (var index in items) {
html = $("<option></option>").text(index).val(items[index]);
sel.append(html);
html = "";
}
jsFiddle http://jsfiddle.net/FXta5/
jsFiddle http://jsfiddle.net/FXta5/
#6
0
You can certainly do this manually, but for fun, this is the sort of thing jQuery Templates are designed to do. The {{each}}
tag will let you iterate over an array or object, building some dynamic HTML as you go.
你当然可以手动执行此操作,但为了好玩,这是jQuery模板设计用来做的事情。 {{each}}标记将允许您遍历数组或对象,随时构建一些动态HTML。
First set up the template:
首先设置模板:
<script id="optionTemplate" type="text/x-jquery-tmpl">
{{each(lang,abbrev) $data}}
<option value="${abbrev}">${lang}</option>
{{/each}}
</script>
Here's the empty HTML <select>
to be filled with new <option>
tags:
这是用
<select id="langsel" name="selector">
<option value="">Select a language</option>
</select>
Your language object:
你的语言对象:
var langs = {
'AFRIKAANS': 'af',
'ALBANIAN': 'sq',
'AMHARIC': 'am',
'ARABIC': 'ar',
'ARMENIAN': 'hy',
'AZERBAIJANI': 'az'
};
Then the easy part:
那么简单的部分:
$("#optionTemplate").tmpl(langs).appendTo("#langsel");
Here's a working example: http://jsfiddle.net/redler/nHtH3/
这是一个有效的例子:http://jsfiddle.net/redler/nHtH3/
#1
9
$(function() {
var languages = {
'AFRIKAANS' : 'af',
'ALBANIAN' : 'sq',
'AMHARIC' : 'am',
'ARABIC' : 'ar',
'ARMENIAN' : 'hy',
'AZERBAIJANI' : 'az'
}
var sel = $('#langsel');
$.each(languages, function(key, value) {
sel.append('<option value="'+value+'">'+key+'</option>');
});
});
Working Fiddle: http://jsfiddle.net/qtb6S/
工作小提琴:http://jsfiddle.net/qtb6S/
#2
2
The answer is here: What is the best way to add options to a select from an array with jQuery?
答案在这里:使用jQuery从数组中向select中添加选项的最佳方法是什么?
Loop through your languages in a for loop, and use the code in that answer above.
在for循环中循环访问您的语言,并使用上面答案中的代码。
#3
1
var languages = [
'AFRIKAANS : af',
'ALBANIAN : sq',
'AMHARIC : am',
]
$.each(languages,function(index,elem){
$("#langsel").append(
$("<option/>").val(elem.split(':')[1]).text(elem.split(':')[0])
);
});
fiddle http://jsfiddle.net/3dFgg/
小提琴http://jsfiddle.net/3dFgg/
#4
0
The quickest (to type) way to do it would be to first build the HTML string, and then insert it into the <select>
:
最快(要键入)的方法是首先构建HTML字符串,然后将其插入
var languages = { /* ... */ },
str = '',
hop = Object.prototype.hasOwnProperty,
x;
for (x in languages) {
if (hop.call(languages, x)) {
str += '<option value="' + languages[x] + '">' + x + '</option>';
}
}
document.getElementById('langsel').innerHTML += str;
#5
0
You could do something like this:
你可以这样做:
var sel = $("#langsel");
var html = "";
var items = Languages;
html = $("<option></option>").text("Select a Language").val("");
sel.append(html);
html = "";
for (var index in items) {
html = $("<option></option>").text(index).val(items[index]);
sel.append(html);
html = "";
}
jsFiddle http://jsfiddle.net/FXta5/
jsFiddle http://jsfiddle.net/FXta5/
#6
0
You can certainly do this manually, but for fun, this is the sort of thing jQuery Templates are designed to do. The {{each}}
tag will let you iterate over an array or object, building some dynamic HTML as you go.
你当然可以手动执行此操作,但为了好玩,这是jQuery模板设计用来做的事情。 {{each}}标记将允许您遍历数组或对象,随时构建一些动态HTML。
First set up the template:
首先设置模板:
<script id="optionTemplate" type="text/x-jquery-tmpl">
{{each(lang,abbrev) $data}}
<option value="${abbrev}">${lang}</option>
{{/each}}
</script>
Here's the empty HTML <select>
to be filled with new <option>
tags:
这是用
<select id="langsel" name="selector">
<option value="">Select a language</option>
</select>
Your language object:
你的语言对象:
var langs = {
'AFRIKAANS': 'af',
'ALBANIAN': 'sq',
'AMHARIC': 'am',
'ARABIC': 'ar',
'ARMENIAN': 'hy',
'AZERBAIJANI': 'az'
};
Then the easy part:
那么简单的部分:
$("#optionTemplate").tmpl(langs).appendTo("#langsel");
Here's a working example: http://jsfiddle.net/redler/nHtH3/
这是一个有效的例子:http://jsfiddle.net/redler/nHtH3/