[英]Auto populate select box
I am trying to auto populate a select box using Jquery and JavaScript. 我正在尝试使用Jquery和JavaScript自动填充选择框。
I done some searching and came across this question and followed the advice of the selected answer. 我做了一些搜索并遇到了这个问题,并按照所选答案的建议。
I originally set up a jsFiddle to illustrate my problem: http://jsfiddle.net/GJdBR/ 我最初设置一个jsFiddle来说明我的问题: http : //jsfiddle.net/GJdBR/
After receiving some advice on answers below I created a new jsfiddle and it worked. 在收到以下答案的一些建议后,我创建了一个新的jsfiddle并且它有效。 http://jsfiddle.net/2UbqP/
http://jsfiddle.net/2UbqP/
However, on my computer I am using a clean install of windows 7, all I have done so far, is install chrome, apanta and xampp. 但是,在我的计算机上,我使用的是Windows 7的干净安装,到目前为止我所做的只是安装chrome,apanta和xampp。 I go to localhost/website and the site comes up, however the js functionality doesn't work, the select box isn't being filled with the var even though I proved the code is correct because of the jsfiddle above.
我转到localhost / website并且站点出现,但是js功能不起作用,即使我证明代码是正确的,因为上面的jsfiddle,选择框没有填充var。
I am getting an error on this: 我收到一个错误:
$(document).ready(function() {
populateTranslationOptions();
});
The error reads: 错误如下:
Uncaught SyntaxError: Unexpected token ILLEGAL
$.each(translationOptions, function (index, value) {
$('#translationOptions')
.append($("<option></option>")
.attr("value",value)
.text(value));
});
You've tried 你试过了
$.each(translationOptions, (value) )
it's wrong, because correct structure is $.each(mapData, callbackFunction)
, here mapData
means Object or Array. 这是错误的,因为正确的结构是
$.each(mapData, callbackFunction)
,这里mapData
表示Object或Array。
And next issue is: 接下来的问题是:
$(document).ready(function() {
populateTranslationOptions;
});
It should ne 它应该是
$(document).ready(function() {
populateTranslationOptions(); // you missed () here
});
Mind that, populateTranslationOptions
doesn't call a function, so to call a function you need to populateTranslationOptions()
. 请注意,
populateTranslationOptions
不会调用函数,因此要调用一个函数,需要populateTranslationOptions()
。
The () after a function means to execute the function itself and return it's value.
函数后面的()表示执行函数本身并返回它的值。 Without it you simply have the function, which can be useful to pass around as a callback.
如果没有它,您只需拥有该函数,该函数可作为回调传递。
Related refs: 相关参考:
You have several problems in your code. 您的代码中有几个问题。 check this
检查一下
function populateTranslationOptions ()
{
var translationOptions = ["Egyptian Hieroglyphs", "Al Bhed (Final Fantasy X)", "Futurama"];
$.each(translationOptions ,function(i,value) {
$('#translationOptions')
.append($("<option></option>")
.attr("value",value)
.text(value));
});
}
$(document).ready(function() {
populateTranslationOptions();
});
Problems in your code: 代码中的问题:
$.each syntax is wrong $ .each语法错误
$.each(translationOptions (value)) { $ .each(translationOptions(value)){
Should be 应该
$.each(translationOptions ,function(i,value) {
populateTranslationOptions;
populateTranslationOptions;
函数populateTranslationOptions;
doesnt call the function populateTranslationOptions();
populateTranslationOptions();
does. For arrays, $.each is unnecessary. 对于数组,$ .each是不必要的。 You can use a simple loop rather.
你可以使用一个简单的循环。 Like as follows -
如下 -
function populateTranslationOptions()
{
var translationOptions = ["Egyptian Hieroglyphs", "Al Bhed (Final Fantasy X)", "Futurama"];
for (var i = 0; i < translationOptions.length; i++) {
$('#translationOptions')
.append('<option value="' + translationOptions[i] + '">' + translationOptions[i] + '</option>');
};
}
$(document).ready(function() {
populateTranslationOptions();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.