对象数组,javascript。- JSON

我只是想知道是否可以简化此操作,使用类,避免使用某些标签中的 id,或者从此表单自动生成 JSON 对象。


<div id="formulario1">

    <label class="elemento">#Manzanas<input type="checkbox" id="elem1"><input type="number" id="val1"></label>

    <label class="elemento">#Bananas<input type="checkbox" id="elem2"><input type="number" id="val2"></label>

    <label class="elemento">#Peras<input type="checkbox" id="elem3"><input type="number" id="val3"></label>

    <label class="elemento">#Naranjas<input type="checkbox" id="elem4"><input type="number" id="val4"></label>

</div>

<button id="enviar">enviar</button>

<script>

    const a = document.getElementById("formulario1");

    var arr = [

        elem1 = {

            check: document.getElementById("elem1").checked,

            value: document.getElementById("val1").value,

        },elem2 = {

            check: document.getElementById("elem2").checked,

            value: document.getElementById("val2").value,

        },elem3 = {

            check: document.getElementById("elem3").checked,

            value: document.getElementById("val3").value,

        },elem4 = {

            check: document.getElementById("elem4").checked,

            value: document.getElementById("val4").value,

        }

    ];


    const boton = document.getElementById("enviar");

    boton.addEventListener("click", function(){

        console.log(arr[0]);

        console.log(arr[1]);

        console.log(arr[2]);

        console.log(arr[3]);

        var arr_json = (JSON.stringify(arr));

        console.log(arr_json);

    });

<script>


12345678_0001
浏览 66回答 5
5回答

芜湖不芜

document.getElementById为了避免其中出现太多 s,您可以做的一件事是使用 usedocument.querySelectorAll来代替,它将返回一个 HTML 节点数组。这将返回所有匹配的节点,您可以迭代这些节点来获取值。在您共享的 HTML 中,document.querySelectorAll('#formulario1 input')将为您提供所有输入,但我鼓励您使用nameHTML 输入上的属性,以便您了解这些值代表什么。

有只小跳蛙

我知道您是 JS 新手,而这正是我刚开始担任 Web 开发人员时的情况。您的脚本可以通过创建一个函数来初始化元素来重构,而不是创建每个元素。您可以简单地使用 for 循环来做到这一点。请查看我的快速重构代码<div id="formulario1">&nbsp; <label class="elemento">#Manzanas<input type="checkbox" id="elem1"><input type="number" id="val1"></label>&nbsp; <label class="elemento">#Bananas<input type="checkbox" id="elem2"><input type="number" id="val2"></label>&nbsp; <label class="elemento">#Peras<input type="checkbox" id="elem3"><input type="number" id="val3"></label>&nbsp; <label class="elemento">#Naranjas<input type="checkbox" id="elem4"><input type="number" id="val4"></label></div><button id="enviar">enviar</button><script>&nbsp; const a = document.getElementById("formulario1");&nbsp; //Old code&nbsp; // var arr = [&nbsp; // elem1 = {&nbsp; // check: document.getElementById("elem1").checked,&nbsp; // value: document.getElementById("val1").value,&nbsp; // },&nbsp; // elem2 = {&nbsp; // check: document.getElementById("elem2").checked,&nbsp; // value: document.getElementById("val2").value,&nbsp; // },&nbsp; // elem3 = {&nbsp; // check: document.getElementById("elem3").checked,&nbsp; // value: document.getElementById("val3").value,&nbsp; // },&nbsp; // elem4 = {&nbsp; // check: document.getElementById("elem4").checked,&nbsp; // value: document.getElementById("val4").value,&nbsp; // }&nbsp; // ];&nbsp; //=========================&nbsp; //refactored version&nbsp; var arr = [];&nbsp; for (var i = 0; i < 4; i++) {&nbsp; &nbsp; arr.push(createElem(i + 1));&nbsp; }&nbsp; const boton = document.getElementById("enviar");&nbsp; //old code&nbsp; &nbsp;&nbsp; // boton.addEventListener("click", function() {&nbsp; // console.log(arr[0]);&nbsp; // console.log(arr[1]);&nbsp; // console.log(arr[2]);&nbsp; // console.log(arr[3]);&nbsp; // var arr_json = (JSON.stringify(arr));&nbsp; // console.log(arr_json);&nbsp; // });&nbsp; //refactored version&nbsp; boton.addEventListener("click", function() {&nbsp; &nbsp; for(var i=0; i<arr.length; i++){&nbsp; &nbsp; &nbsp; console.log(arr[i]);&nbsp; &nbsp; }&nbsp; &nbsp; var arr_json = (JSON.stringify(arr));&nbsp; &nbsp; console.log(arr_json);&nbsp; });&nbsp;// new function to create Element&nbsp; function createElem(index) {&nbsp; &nbsp; const elem = {&nbsp; &nbsp; &nbsp; check: document.getElementById(i).checked,&nbsp; &nbsp; &nbsp; value: document.getElementById(i).value,&nbsp; &nbsp; }&nbsp; &nbsp; return elem;&nbsp; }</script>或可运行版本&nbsp;https://jsfiddle.net/x40whk5d/2/还有其他一些地方可以改进,但我相信当你以后有了更深入的理解后,你自己就能很容易地找到它。我已经注释掉了您的代码并添加了我的新代码以帮助您获得更好的视图。谢谢,祝你好运!

ABOUTYOU

我用 JS 类做了一个解决方案,没有所有 CSS 类或 Id,结果数据是这样的:{&nbsp; "input0":{"checkbox":true,"input":"23423"},&nbsp; "input1":{"checkbox":false,"input":""},&nbsp; "input2":{"checkbox":false,"input":""},&nbsp; "input3":{"checkbox":false,"input":""}}&nbsp;在这里你可以看到codesanbox中的解决方案

开满天机

您可以枚举表单元素中所有可用的复选框。请参见下面的示例:function loopForm(form) {var arr = [];for (var i = 0; i < form.elements.length; i++) {&nbsp; &nbsp; if (form.elements[i].type == 'checkbox') {&nbsp; &nbsp; &nbsp; &nbsp; arr.push({ check: form.elements[i].checked, value: form.elements[i].value });&nbsp; &nbsp; }}var arr_json = (JSON.stringify(arr));document.getElementById("demo").innerHTML = arr_json;}

森林海

有效。这是我正在寻找的输出。谢谢!const contenedor = document.getElementById('contenedor');&nbsp; &nbsp; &nbsp; &nbsp; const elem = contenedor.querySelectorAll('.caja');&nbsp; &nbsp; &nbsp; &nbsp; const elem2 = contenedor.querySelectorAll('.cajas');&nbsp; &nbsp; document.getElementById('accion').addEventListener('click', function(){&nbsp; &nbsp; &nbsp; &nbsp; var i = 0; //variables&nbsp; &nbsp; &nbsp; &nbsp; let arr = [];&nbsp; &nbsp; &nbsp; &nbsp; for(i; i< elem.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr.push({texto:elem[i].value, valor: elem2[i].value});&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; console.log(JSON.stringify(arr));&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5