如何用JavaScript加载外部文件?

时间:2021-04-20 00:12:32

I am writing a script where you can add and remove a drop-down list of languages. I got it working but my question is if there is a way to externalize the select tag part of the code as I would have more than 100 options and load it in JavaScript when a link is click. I don't want to have 100 option tags within the script. On the PHP side I use the the include statement to load my list of options.

我正在编写一个脚本,您可以在其中添加和删除语言的下拉列表。我让它工作了,但我的问题是,是否有一种方法可以将代码的select标记部分具体化,因为我将有超过100个选项,并在单击链接时将其加载到JavaScript中。我不想在脚本中有100个选项标签。在PHP方面,我使用include语句来加载选项列表。

This is where my problem is.

这就是我的问题所在。

$(function() {
    var scntDiv = $('#container');
    var i = $('#container p').size() + 1;

    $('#addScnt').live('click', function() {
        $('<p><select>I DONT WANT TO HAVE 100 OPTION TAGS HERE</select></p>').appendTo(scntDiv);
        i++;
        return false;
    });
});

here is my code that runs with a few option tags in the script.

下面是我的代码,它在脚本中使用了一些选项标签。

Full code.

完整的代码。

3 个解决方案

#1


2  

You can put all your data into an Json file like below(For example):

您可以将所有数据放入如下所示的Json文件(例如):

{"student": [
  {
    "id": "1",
    "name": "Person1"
  },
  {
    "id": "2",
    "name": "Person2"
  },
  {
    "id": "3",
    "name": "Person3"
  }
]}

Now on click Implement the following

现在单击“实现”

     $('#addScnt').on('click', function() {
     //get a reference to the select element
$('<p><select id="test"></select></p>').appendTo(scntDiv);
        var $select = $('#test`enter code here`');</code>

        //request the JSON data and parse into the select element
        $.getJSON('student.JSON', function(data){

          //clear the current content of the select
          $select.html('');

          //iterate over the data and append a select option
          $.each(data.student, function(key, val){ 
            $select.append('<option id="' + val.id + '">' + val.name + '</option>');
          })
        });
        });

#2


4  

You can store your languages as objects. It can be either a static file or dynamically generated response. Then, you can use it for dynamical options creation:

您可以将您的语言存储为对象。它可以是静态文件,也可以是动态生成的响应。然后,你可以用它来创建动态选项:

MyLanguages.json:

MyLanguages.json:

[
    {
        'Name': 'English',
        'Sign': 'EN'
    },
    {
        'Name': 'Spanish',
        'Sign': 'ES'
    },
    {
        'Name': 'Russian',
        'Sign': 'RU'
    }
]

Your page:

你的页面:

$.ajax({
    url: './MyLanguages.json',
    dataType: 'json'
}).done(function(data) {
  var $select = $("select");

  $(data).each(function() {
    $("<option/>").text(this.Name).val(this.Sign).appendTo($select);
  });
});

#3


3  

You can put the options in a JSON file and load them using AJAX (http) request.

您可以将选项放入JSON文件中,并使用AJAX (http)请求加载它们。

#1


2  

You can put all your data into an Json file like below(For example):

您可以将所有数据放入如下所示的Json文件(例如):

{"student": [
  {
    "id": "1",
    "name": "Person1"
  },
  {
    "id": "2",
    "name": "Person2"
  },
  {
    "id": "3",
    "name": "Person3"
  }
]}

Now on click Implement the following

现在单击“实现”

     $('#addScnt').on('click', function() {
     //get a reference to the select element
$('<p><select id="test"></select></p>').appendTo(scntDiv);
        var $select = $('#test`enter code here`');</code>

        //request the JSON data and parse into the select element
        $.getJSON('student.JSON', function(data){

          //clear the current content of the select
          $select.html('');

          //iterate over the data and append a select option
          $.each(data.student, function(key, val){ 
            $select.append('<option id="' + val.id + '">' + val.name + '</option>');
          })
        });
        });

#2


4  

You can store your languages as objects. It can be either a static file or dynamically generated response. Then, you can use it for dynamical options creation:

您可以将您的语言存储为对象。它可以是静态文件,也可以是动态生成的响应。然后,你可以用它来创建动态选项:

MyLanguages.json:

MyLanguages.json:

[
    {
        'Name': 'English',
        'Sign': 'EN'
    },
    {
        'Name': 'Spanish',
        'Sign': 'ES'
    },
    {
        'Name': 'Russian',
        'Sign': 'RU'
    }
]

Your page:

你的页面:

$.ajax({
    url: './MyLanguages.json',
    dataType: 'json'
}).done(function(data) {
  var $select = $("select");

  $(data).each(function() {
    $("<option/>").text(this.Name).val(this.Sign).appendTo($select);
  });
});

#3


3  

You can put the options in a JSON file and load them using AJAX (http) request.

您可以将选项放入JSON文件中,并使用AJAX (http)请求加载它们。