自动填充选择框 [英] Auto populate select box

查看:89
本文介绍了自动填充选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jquery和JavaScript自动填充选择框。



我做了一些搜索并遇到了这个问题并遵循所选答案的建议。



<我最初设置了一个jsFiddle来说明我的问题: http://jsfiddle.net/GJdBR/



在收到以下答案的一些建议后,我创建了一个新的jsfiddle并且它有效。 http://jsfiddle.net/2UbqP/



<但是,在我的电脑上,我使用的是Windows 7的干净安装,到目前为止我所做的一切,都是安装chrome,apanta和xampp。我转到localhost / website并且站点出现,但是js功能不起作用,即使我证明代码是正确的,因为上面的jsfiddle,选择框没有填充var。



我收到错误:

  $(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();
});

代码中的问题:




  • < a href =http://api.jquery.com/jQuery.each/ =nofollow> $。每个语法错误



    $ .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 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(translationOptions (value)) {

Should be

$.each(translationOptions ,function(i,value) {

  • You were not calling the function populateTranslationOptions; doesnt call the function populateTranslationOptions(); does.

Working Fiddle

这篇关于自动填充选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆