【发布时间】:2017-05-20 02:50:18
【问题描述】:
我有很多不同的 .json 文件,我想将它们的名称输入到 HTML 页面(本地)表单中,以便将此信息加载到表中。
我的 HTML 表单如下所示:
<form>
File: <input type="text" Id="file_name">
<input type="submit" value="Load file" onclick="filltable()">
</form>
我的表头是这样的:
<div class="container">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Sample</th>
<th>Variant</th>
<th>Depth</th>
</tr>
</thead>
</table>
</div>
我的 javascript 脚本是这样的:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
function filltable() {
var name = document.getElementById("file_name").value +".json";
};
$.getJSON(name,function(stuff){
var items = [];
$.each(stuff, function(key, val){
items.push("<tr>");
items.push("<td id=''"+key+"''>"+val.sample+"</td>");
items.push("<td id=''"+key+"''>"+val.variant+"</td>");
items.push("<td id=''"+key+"''>"+val.depth+"</td>");
});
$("<tbody/>", {html: items.join("")}).appendTo("table");
});
</script>
我还尝试将 .getJSON 代码嵌入到我的 filltable() 函数中,以便在单击提交按钮时调用它 - 但这不起作用。如果我更换:
"$.getJSON(name,function(stuff){"
与:
".getJSON("file1.json",function(stuff{"
然后它加载正常 - 我只想能够使用输入加载不同的文件。
【问题讨论】:
-
fillTable()没有做任何事情,因为它在你做var name =之后结束。 -
好的,但我确实得到了变量“名称”,就像我做 console.log 或警报(名称)一样,那么它就是我在输入中输入的任何内容 - 那么为什么,如果我嵌入 $.get .JSON 代码到那个函数中它不会填满表格吗?
-
当我将 .json 文件放在文件路径中时,$get.JSON 代码可以正常工作——只是当我将它与 name 变量一起使用时就不行
-
尝试使用从表对象调用的原生
insertRow() -
还要确保在
file_input文本框中只输入file1而不是file1.json
标签: javascript php jquery html json