如何在函数上使用 JavaScript 的 JSON 文件两次
how use JSON file using javascript on a function twice
我正在尝试创建一个可以加载的网页,它从.js文件创建数据并填充到下拉列表中。
当用户选择数据时,它会将输出带入输出字段。我怎么可能这样做?请帮忙
Js 文件在这里"http://www.w3schools.com/json/myTutorials.js"
<!DOCTYPE html>
<html>
<body>
<select id="id01" onchange="getURL()"></select >
<p id="output"></p>
<script>
function myFunction(arr) {
var out = "";
var i;
for(i = 0; i<arr.length; i++) {
out += '<option>' + arr[i].url + '</option>';
}
document.getElementById("id01").innerHTML = out;
}
function getURL(){
myFunction();
//the display and url should be placed on output field <p>
}
</script>
<script src="myTutorials.js"></script>
</body>
</html>
请阅读代码和注释,如果不清楚,请告诉我。
// from the file
var arr = [
{
"display": "HTML Tutorial",
"url": "http://www.w3schools.com/html/default.asp"
},
{
"display": "CSS Tutorial",
"url": "http://www.w3schools.com/css/default.asp"
},
{
"display": "JavaScript Tutorial",
"url": "http://www.w3schools.com/js/default.asp"
},
{
"display": "jQuery Tutorial",
"url": "http://www.w3schools.com/jquery/default.asp"
},
{
"display": "JSON Tutorial",
"url": "http://www.w3schools.com/json/default.asp"
},
{
"display": "AJAX Tutorial",
"url": "http://www.w3schools.com/ajax/default.asp"
},
{
"display": "SQL Tutorial",
"url": "http://www.w3schools.com/sql/default.asp"
},
{
"display": "PHP Tutorial",
"url": "http://www.w3schools.com/php/default.asp"
},
{
"display": "XML Tutorial",
"url": "http://www.w3schools.com/xml/default.asp"
}
];
function myFunction(arr) {
var out = "";
var i;
out += '<option>Pick a website</option>';
for(i = 0; i<arr.length; i++) {
/* set the value as the URL, and the text as the display property to thow clean text for the user
and functionality text to the browser.*/
out += '<option value="' + arr[i].url + '">' + arr[i].display + '</option>';
}
document.getElementById("id01").innerHTML = out;
}
// when user pick an option (onchange), show the select's value in the #output
function getURL(select) {
document.getElementById('output').innerHTML = select.value;
}
// call the fill function on body ready
myFunction(arr);
<select id="id01" onchange="getURL(this)"></select>
<p id="output"></p>
更新
在下面的示例中,我们使用 http://www.w3schools.com/json/myTutorials.js 脚本来填充数据
注意:您必须了解这是一个非常具体的方案。在现实世界中,您不会获得这样的文件(调用特定函数)。在大多数情况下,您需要使用ajax
从外部资源获取数据,这是完全不同的事情。阅读更多关于 ajax 和 ajax with jQuery。
<script>
function myFunction(arr) {
var out = "";
var i;
out += '<option>Pick a website</option>';
for(i = 0; i<arr.length; i++) {
/* set the value as the URL, and the text as the display property to thow clean text for the user
and functionality text to the browser.*/
out += '<option value="' + arr[i].url + '">' + arr[i].display + '</option>';
}
document.getElementById("id01").innerHTML = out;
}
// when user pick an option (onchange), show the select's value in the #output
function getURL(select) {
document.getElementById('output').innerHTML = select.value;
}
</script>
<select id="id01" onchange="getURL(this)"></select>
<p id="output"></p>
<script src="http://www.w3schools.com/json/myTutorials.js"></script>
相关文章:
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 不显示带有本地json文件数据的谷歌地图脚本
- 可以't使用Appcelerator将JSON文件解析为JavaScript中的TableView
- 加载两个具有相同父密钥名称的json文件
- 使用JSON文件中的变量(字符串)填充文本区域
- 动态显示JSON文件内容
- 将JSON转换为放入Sdcard中的CSV文件
- 正在尝试将JSON文件放入JS数组
- 如何检查Json文件更新,如果更新了,则用更新的数据刷新我的页面
- 单击按钮更改加载到表中的JSON文件
- 创建一个包含两个文件 json 的 Angularjs 过滤器
- Multer上传文件+JSON对象
- 使用node.js读取和写入文件(JSON)
- 如何在angularjs中设置变量's值来上传文件json数据
- 向现有文件. Json写入Json,没有“语法错误”
- 如何保存更新json(文件json)与jquery或javascript
- AngularJS:当使用$http请求一个文件(json content)时发生SyntaxError
- 如何简化此代码?将文件json转换为数组json
- 如何从外部文件JSON创建JAVASCRIPT数组