如何将JSON数据导入Javascript表?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将JSON数据导入Javascript表?相关的知识,希望对你有一定的参考价值。
我正在尝试使用javascript将JSON文件中的数据带到表的输入中,以便可以从表中编辑相同的输入,但是只能从JSON文件中获取最后的数据(值10和rat10)我尝试了其他领域,但他没有将它们带给我。[在此处输入图片描述] [1]Parte del Script
const xhttp = new XMLHttpRequest();
xhttp.open('GET', 'Datos/datos.json', true);
xhttp.send();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)
//console.log(this.responseText);
let datos = JSON.parse(this.responseText);
// console.log(datos);
for(let item of datos)
document.getElementById('valor').value = `$item.valor`;
document.getElementById('resultado').value = `$item.resultado`;
document.getElementById('valor5').value = `$item.valor5`;
document.getElementById('rat5').value = `$item.rat5`;
document.getElementById('valor10').value = `$item.valor10`;
document.getElementById('rat10'[enter image description here][2]).value = `$item.rat10`;
JSON
[
"valor":"123","resultado":"2","resultado2":"61.5",
"valor1":"34","rat1":"2","result":"17",
"valor2":"12","rat2":"2","result2":"6",
"valor3":"12","rat3":"4","resultado3":"3",
"valor4":"23","rat4":"2","resultado4":"11.5",
"valor5":"23","rat5":"4","resultado5":"5.75",
"valor6":"21","rat6":"2","resultado6":"10.5",
"valor7":"5","rat7":"7","resultado7":"0.7142857142857143",
"valor8":"6","rat8":"3","resultado8":"2",
"valor9":"23","rat9":"2","resultado9":"11.5",
"valor10":"34","rat10":"4","resultado10":"8.5"
]
<td rowspan="8" style="text-align: center; background-color: rgba(7, 129, 7, 0.705);"><form action="Datos/datos.php" id="formdata"><input
type="number" class="valor" id="valor" name="valor" ></form></td>
<td rowspan="8" style="text-align: center; background-color: rgba(216, 187, 22, 0.911);"><input
type="number" class="resultado" id="resultado" name="resultado" ></td>
<td style="text-align: center; background-color: rgba(7, 129, 7, 0.705);"><Input type="number"
id="valor5" name="valor5" > </td> <td
style="text-align: center; background-color: rgba(216, 187, 22, 0.911);"><Input
type="number" id="rat5" name="rat5"></Input></td>
[2]: https://i.stack.imgur.com/w4VX5.png
答案
对不起,无法评论...
您想要实现什么?
您能否提供更多代码?`
是
document.getElementById('rat10')
此元素确实可用吗?
顺便说一句,您之所以只获取最新数据是因为您没有将其保存在外部(全局)
以上是关于如何将JSON数据导入Javascript表?的主要内容,如果未能解决你的问题,请参考以下文章
如何将 .JSON 文件导入 Oracle SQL Developer
使用两个 SQL 表为 Javascript 获取 JSON 数据