本文介绍了自动填充选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试使用Jquery和JavaScript自动填充选择框。 我做了一些搜索并遇到了这个问题并遵循所选答案的建议。 http://jsfiddle.net/GJdBR/ 在收到以下答案的一些建议后,我创建了一个新的jsfiddle并且它有效。 http://jsfiddle.net/2UbqP/ 我收到错误: $(document).ready( function(){ populateTranslationOptions(); }); 错误如下: 未捕获的SyntaxError:意外的令牌ILLEGAL 解决方案 function populateTranslationOptions() { var translationOptions = [Egyptian Hieroglyphs,Al Bhed(最终幻想X),Futurama]; $ .each(translationOptions,function(i,value){ $('#translationOptions') .append($(< option>< / option>) .attr(value,value) .text(value)); }); } $(文件).ready(function(){ populateTranslationOptions(); }); 代码中的问题: $。每个语法错误 $ .each(translationOptions(value)){ 应该是 $。each(translationOptions,function(i,value){ 你没有调用函数 populateTranslationOptions; 没有调用函数 populateTranslationOptions() ; 确实。 工作小提琴 I am trying to auto populate a select box using Jquery and 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/After receiving some advice on answers below I created a new jsfiddle and it worked. 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. 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.I am getting an error on this:$(document).ready(function() { populateTranslationOptions();});The error reads:Uncaught SyntaxError: Unexpected token ILLEGAL 解决方案 You have several problems in your code. check thisfunction 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(translationOptions (value)) {Should be$.each(translationOptions ,function(i,value) {You were not calling the function populateTranslationOptions; doesnt call the functionpopulateTranslationOptions(); does.Working Fiddle 这篇关于自动填充选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 12:42
查看更多