简体   繁体   English

JavaScript 后多个输入同名

[英]JavaScript post same name for multiple inputs

I want to upload in the database using pure javascript in my following input fields.我想在我的以下输入字段中使用纯 javascript 上传到数据库中。

 var i = 2; var id=1; function addkid() { var div = document.createElement('div'); console.log('my id cj '+id) div.innerHTML = 'Day' + id + ': <input type="text" name="child_' + id + '"/>' + '<input type="button" class="submit_but" id="rem_kid()_' + id + '" onclick="remkid(this)" value="-" />'; //div.innerHTML = 'Day' + id + ': <input type="text" name="child_' + id + '"/>' + ' <input type="button" id="add_kid()_' + id + '" onclick="addkid()" value="+" />' + ' <input type="button" id="rem_kid()_' + id + '" onclick="remkid(this)" value="-" />'; // event.stopPropagation(); document.getElementById('kids').appendChild(div); id++; } function remkid(div) { document.getElementById('kids').removeChild(div.parentNode); i--; } ///////////////////////////////////////// var i = 1; var idd = 1; function addmen() { var div = document.createElement('div'); console.log('my id cj '+idd); //var id = i; // alert(id); div.innerHTML = 'img' + idd + ': <input type="file" name="child_' + id + '"/>' + '<input type="button" class="submit_but" id="rem_kid()_' + id + '" onclick="remmen(this)" value="-" />'; //div.innerHTML = 'Day' + id + ': <input type="text" name="child_' + id + '"/>' + ' <input type="button" id="add_kid()_' + id + '" onclick="addkid()" value="+" />' + ' <input type="button" id="rem_kid()_' + id + '" onclick="remkid(this)" value="-" />'; document.getElementById('menand').appendChild(div); idd++; } function remmen(div) { document.getElementById('menand').removeChild(div.parentNode); i--; }
 Title: <input type="text" name="name"> <br/> <div id="kids"> Day/s: <:--<input type="text" name="child_1">--> <input type="button" class="submit_but" id="add_kid()_1" onclick="addkid()" value="Add tour day" /><br> </div> <div id="menand"> Img/s: <!--<input type="file" name="img">--> <input type="button" class="submit_but" id="add_kid()_1" onclick="addmen()" value="Add tour Image" /><br> </div> <div> <input type="button" value="Save" name="Save" class="submit_but" onclick="saveroute()"> </div>

I want to know how to upload this multiple same name input field into the database.我想知道如何将这个多个同名输入字段上传到数据库中。 I'm never doing this type of code in before.我以前从未做过这种类型的代码。 please help solve my problem请帮助解决我的问题

The id is an unique identifier. id 是唯一标识符。 The means that your 'id=add_kid()_1' can't repeat for more than one.这意味着您的 'id=add_kid()_1' 不能重复超过一个。 The class attribute can repeat. class 属性可以重复。 In this case, you'll use the class="submit_but" to catch the two input tags.在这种情况下,您将使用 class="submit_but" 来捕获两个输入标签。

Then, the first step is extract the info.然后,第一步是提取信息。 There's only two inputs to extract info from and they have the tag in common: "submit_but".只有两个输入可以从中提取信息,并且它们具有共同的标签:“submit_but”。

const infoElements = document.querySelector('.submit_but');

infoElemnts contains all nodes with class="submit_but". infoElemnts 包含所有具有 class="submit_but" 的节点。

Here your answer pt1 If you wanna store the info of inputs in an array:这是你的答案 pt1如果你想将输入的信息存储在一个数组中:

 const infoValues = infoElements.map((element) => {
        return element.value;
    }

Now, infoValues has all inputs values.现在, infoValues 具有所有输入值。 You can do all this process with yours childs inputs that will be created.您可以使用将要创建的孩子的输入来完成所有这些过程。

 div.innerHTML = '<input class="dayTour"></input>' 
 div.innerHTML = '<input class="whatever"></input>' 

 const dayTourElements = document.querySelector('.dayTour');
 
 const dayTourValues = dayTourElements.map((element) => {
        return element.value;
 }

Here your answer pt2这是你的答案pt2

I don't how you wanna save this, but for simple approach you can simple write it to your local storage into a '.txt' file.我不知道您想如何保存它,但是对于简单的方法,您可以将其简单地写入本地存储到“.txt”文件中。

const fs = require('fs');

fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
  if (err) throw err;
  console.log('Saved!');
});

Here tutorial: https://www.w3schools.com/nodejs/nodejs_filesystem.asp这里教程: https://www.w3schools.com/nodejs/nodejs_filesystem.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM