英文:
When a user inputs a value, how can I create multiple elements at the same time using JavaScript?
问题
我计划自动生成多个用户输入框。
例如:
如果用户输入10,每次自动生成3个输入框。所以用户会得到30个输入框。
我整天都在寻找解决方案,但还是没有成功。在我循环这段代码之前,它是可以工作的。但是在我循环之后,它就不再工作了。
这是我在循环之前得到的结果。CSS已经预先链接,所以它在一行上对齐。
// 仅获取一次输入值
const inputValue = document.querySelector(".inputBox").value;
// 在循环之外定义函数
function btnClick(){
// 循环直到达到输入值
let i = 0;
while (i < inputValue) {
// 创建一个新的div元素
const divElement = document.createElement("div");
const sp2 = document.querySelector(".page");
const parentdiv = sp2.parentNode;
parentdiv.insertBefore(divElement, sp2.nextSibling);
// 给新的div元素添加类
document.querySelectorAll("div")[2].classList.add("calBox");
const length = "<p class='first'> Length : <input type='text' class='second' value='' /> </p>";
const height = "<p class='first'> Height : <input type='text' class='second' value='' /> </p>";
const width = "<p class='first'> Width : <input type='text' class='second' value='' /> </p>";
const first = document.querySelector(".calBox");
const second = length + height + width + "<br />";
first.innerHTML = second ;
i++;
}
}
英文:
I'm planning to auto-generate multiple user input boxes.
Example:
If the user inputs 10, auto-generate 3 input fields at each time. So the user gets 30 input fields.
I'm trying to find a solution all day long but it still doesn't work for me.
Before I looped this code it worked.
After I looped it it's no longer working.
This is what I got before looping. CSS was pre-linked that's why it's aligned at one line.
// Get the input value only once
const inputValue = document.querySelector(".inputBox").value;
// Define the function outside of the loop
function btnClick(){
/// Loop until the input value is reached
let i = 0;
while (i < inputValue) {
// Create a new div element
const divElement = document.createElement("div");
const sp2 = document.querySelector(".page");
const parentdiv = sp2.parentNode;
parentdiv.insertBefore(divElement, sp2.nextSibling);
//Adding class to new div element
document.querySelectorAll("div")[2].classList.add("calBox");
const length = "<p class='first'> Length : <input type='text' class='second' value='' /> </p>";
const height = "<p class='first'> Height : <input type='text' class='second' value='' /> </p>";
const width = "<p class='first'> Width : <input type='text' class='second' value='' /> </p>";
const first = document.querySelector(".calBox");
const second = length + height + width +"<br />";
first.innerHTML = second ;
i++;
}
}
答案1
得分: 1
这是一个使用文档片段接口的用例。
let button = document.querySelector("button");
button.addEventListener("click", function(e) {
let quantity = parseInt(document.querySelector("#qty").value);
if (quantity >= 0) {
let fragment = new DocumentFragment();
while (quantity--) {
let input = document.createElement("input");
fragment.append(input);
}
document.body.append(fragment);
}
});
<input type="text" id="qty"><button>enter</button>
英文:
This is a use case for the document fragment interface.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let button = document.querySelector("button");
button.addEventListener("click",function(e){
let quantity = parseInt(document.querySelector("#qty").value);
if(quantity >= 0) {
let fragment = new DocumentFragment();
while( quantity-- ) {
let input = document.createElement("input");
fragment.append(input);
}
document.body.append(fragment);
}
});
<!-- language: lang-html -->
<input type-text id=qty><button>enter
<!-- end snippet -->
答案2
得分: 0
const button = document.querySelector(".btn");
const buttonRest = document.querySelector(".reset");
button.addEventListener("click",function(e){
let quantity = parseInt(document.querySelector("#myNumber").value); //声明输入
if(quantity >= 0) {
for (var i=1; quantity>=i ; i++){
var length = "<p class='first'>"+ i +". 长度 : <input type='number' class='second"+i+"' value='' /> </p>";
var height = "<p class='first'> 高度 : <input type='number' class='second"+i+"' value='' /> </p>";
var width = "<p class='first'> 宽度 : <input type='number' class='second"+i+"' value='' /> </p>";
var inputTotal = length + height + width;
document.querySelector(".calBox").innerHTML += inputTotal;
}
button.disabled=true;
}
});
buttonRest.addEventListener("click", function(){
location.reload(true)
});
Below are HTML code
Total Cargo Box :
这是我为此问题提供的解决方案,感谢 @Ronnie Royston 的代码和思路启发。
我通过他的代码复制和思考出了自己的方法。
希望这篇文章对其他人也有所帮助。
现在我需要考虑关于这个问题的计算过程,希望你们编码愉快。
英文:
const button = document.querySelector(".btn");
const buttonRest = document.querySelector(".reset");
button.addEventListener("click",function(e){
let quantity = parseInt(document.querySelector("#myNumber").value); //Declare input
if(quantity >= 0) {
for (var i=1; quantity>=i ; i++){
var length = "<p class='first'>"+ i +". Length : <input type='number' class='second"+i+"' value='' /> </p>";
var height = "<p class='first'> Height : <input type='number' class='second"+i+"' value='' /> </p>";
var width = "<p class='first'> Width : <input type='number' class='second"+i+"' value='' /> </p>";
var inputTotal = length + height + width;
document.querySelector(".calBox").innerHTML += inputTotal;
}
button.disabled=true;
}
});
buttonRest.addEventListener("click", function(){
location.reload(true)
});
Below are HTML code
<p class="askCargoBox">Total Cargo Box :
<input type="number" id="myNumber" placeholder="Number"/>
<button class="btn"> Comfirm </button>
<button class="reset"> Reset </button>
</p>
This's a solution what i was done for and thanks for @Ronnie Royston
I figure out by his code replicate and thinking my own ways .
I hope this article will help other guy too.
now i need to think about calculation process about this so
hope u guys happy coding
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论