javascript - 在 Javascript 中通过标签名称获取信息
问题描述
我需要一种方法来捕获所有未知数量的文本字段并将它们保存为 JSON:我很擅长编码,但对 web 很陌生。这甚至是这样做的方法吗?谢谢!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="myP">
field
<br />
</div>
<button onclick="addField()">add field</button>
<button onclick="captureAllInfo()">Capture</button>
<script>
var whichdiv = 0;
function addField() {
whichdiv = whichdiv + 1;
parentDocument = document.getElementById("myP");
var mydiv = document.createElement("div");
mydiv.id = whichdiv;
var input1 = document.createElement("input");
var label1 = document.createElement("label");
label1.innerHTML = "input label 1 ";
input1.id = "Info1";
var input2 = document.createElement("input");
var label2 = document.createElement("label");
label2.innerHTML = "imput label 2";
input2.id = "Info2";
mydiv.appendChild(label1);
mydiv.appendChild(input1);
mydiv.appendChild(label2);
mydiv.appendChild(input2);
parentDocument.appendChild(mydiv);
}
function captureAllInfo() {
for (var i = 0; i < whichdiv; i++) {
console.log("loopin");
var thisDiv = document.getElementById(i);
console.log(thisDiv.getElementById("Info1").value);
}
}
</script>
</body>
</html>
我真正想做的是获取这两个输入,然后将它们添加到 JSON 对象,如 JSON STRUCTURE,如下所示:
{
"1":
{input1 : "string",
input2: "String"},
"2":
{input1 : "string",
input2: "String"}
"3"...,
"4"...
}
解决方案
请原谅我的函数格式错误。我在我的 fairphone2 上写了这个。这个想法是我用于className
重复的标识符。另请注意,id
应避免使用纯数字标签。
<html>
<div id="myP">
field
<br />
</div>
<button onclick="addField()">add field</button>
<button onclick="captureAllInfo()">Capture</button>
<script>
var whichdiv = 0;
function addField() {
whichdiv = whichdiv + 1;
parentDocument = document.getElementById("myP");
var mydiv = document.createElement("div");
mydiv.id = 'd'+whichdiv;
mydiv.className='idiv';
var input1 = document.createElement("input");
var label1 = document.createElement("label");
label1.innerHTML = "input label 1 ";
input1.className = "Info1";
var input2 = document.createElement("input");
var label2 = document.createElement("label");
label2.innerHTML = "input label 2";
input2.className = "Info2";
mydiv.appendChild(label1);
mydiv.appendChild(input1);
mydiv.appendChild(label2);
mydiv.appendChild(input2);
parentDocument.appendChild(mydiv);
}
function qsa(s,o){return [...(o||document).querySelectorAll(s)]}
function captureAllInfo() {var o={};
qsa('.idiv').forEach(d=>{
var c=o[d.id.substr(1)]={};
qsa('input',d).forEach(t=>c[t.className]=t.value);
});
console.log(JSON.stringify(o));
}
</script>
</html>
编辑:。qsa()
我为各种“QuerySelectorAll”方法添加了快捷功能,以使所有内容更具可读性。qsa()
返回一个数组而不是一个简单的节点列表。第二个参数qsa()
是可选的。如果给定,它将是.querySelectorAll()
为其调用方法的元素。它默认为document
.
推荐阅读
- angular - Component.providers 和 Component.viewProviders 有什么区别?
- python - pandas 有没有办法在一个数据帧中搜索以确定另一个数据帧中发生的情况?
- python - 使用 pipenv 处理仅开发依赖项
- javascript - 如何绑定函数而不是创建一个新函数?
- intellij-plugin - 如何从 PsiElement 或 VirtualFile 获取 KtClass?
- firebase - Firebase 是否对实际上不更改任何字段值的文档写入收费?
- c# - 使用响应对象将gridview导出到excel
- jquery - jQuery - 将类“活动”添加到菜单上的当前页面
- regex - Visual Basic 2010 中的 Regex.Matches 问题
- php - 如何在 ACF 表单中添加多个提交按钮?