gpt4 book ai didi

javascript - 使用特定 .json 文件的 HTML 表单将 .json 文件加载到 HTML 模板

转载 作者:太空宇宙 更新时间:2023-11-04 16:11:06 24 4
gpt4 key购买 nike

我有一堆不同的 .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(名称,函数(东西){”

与:

".getJSON("file1.json",function(stuff{"

然后它加载正常 - 我只是希望能够使用输入加载不同的文件。

最佳答案

试试这个:

$('form').on('submit', function(){
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");
});
return false;

});

关于javascript - 使用特定 .json 文件的 HTML 表单将 .json 文件加载到 HTML 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41487100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com