如何绑定
我们如何将选择标签与变量数据绑定?...
这是我的示例代码。
<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>
我想使用上述语言绑定我的选择标签
<select id="langsel" name="selector">
<option value="">Select a language</option>
</select>
如何将“langsel”选择与上述变量绑定。?
我需要如下输出:
<select id="langsel" name="selector">
<option value="">Select a language</option>
<option value="af">AFRIKAANS</option>
....
</select>
How we can bind select tag with variable data?..
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.?
I need output like below :
<select id="langsel" name="selector">
<option value="">Select a language</option>
<option value="af">AFRIKAANS</option>
....
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
工作小提琴: http://jsfiddle.net/qtb6S/
Working Fiddle: http://jsfiddle.net/qtb6S/
答案在这里: 使用 jQuery 将选项添加到数组中的选择的最佳方法是什么?
在 for 循环中循环遍历您的语言,并使用上面答案中的代码。
The answer is here: What is the best way to add options to a select from an array with jQuery?
Loop through your languages in a for loop, and use the code in that answer above.
小提琴http://jsfiddle.net/3dFgg/
fiddle http://jsfiddle.net/3dFgg/
最快(输入)的方法是首先构建 HTML 字符串,然后将其插入到
The quickest (to type) way to do it would be to first build the HTML string, and then insert it into the
<select>
:你可以这样做:
jsFiddle http://jsfiddle.net/FXta5/
You could do something like this:
jsFiddle http://jsfiddle.net/FXta5/
您当然可以手动执行此操作,但为了好玩,这就是 jQuery 模板 旨在做到这一点。
{{each}}
标签可让您进行迭代在数组或对象上,构建一些动态 HTML。首先设置模板:
这是空的 HTML
您的语言对象:
然后是简单的部分:
这是一个工作示例: http://jsfiddle.net/redler/nHtH3/
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.First set up the template:
Here's the empty HTML
<select>
to be filled with new<option>
tags:Your language object:
Then the easy part:
Here's a working example: http://jsfiddle.net/redler/nHtH3/