[英]add input type select with options in Javascript
I am creating a order form and i am new programming. 我正在创建订单,我是新编程。 the idea is that the customer can add new box to order a new product with the quantity. 我们的想法是,客户可以添加新的盒子来订购具有数量的新产品。 with this code i can create a box with products but i didn't get to put a quantity box beside of the product box. 使用此代码,我可以创建一个带有产品的盒子,但我没有在产品盒旁边放一个数量框。 I would like to create one for the products and another one for quantity and when the customer click on New Product create a new product list and a quantity list or a quantity field to enter the value. 我想为产品创建一个,为数量创建另一个,当客户点击新产品时,创建新产品列表和数量列表或数量字段以输入值。 I tryed copie the document.create... and change the div, but i don`t know how to direct to another choices. 我试着复制文件。创建...并更改div,但我不知道如何指导另一个选择。 I appreciate if you guys can help-me. 如果你们能帮助我,我感激不尽。 Thank you. 谢谢。
JavaScript JavaScript的
var choices = [
"Product1",
"Product2",
"Product3"];
function addInput(divName) {
var newDiv = document.createElement('div');
var selectHTML = "";
selectHTML="<select>";
for(i = 0; i < choices.length; i = i + 1) {
selectHTML += "<option value='" + choices[i] + "'>" + choices[i] + "</option>";}
selectHTML += "</select>";
newDiv.innerHTML = selectHTML;
document.getElementById(divName).appendChild(newDiv);
var Div = document.createElement('div');
var selectHTML = "";
selectHTML="<select>";
for(i = 0; i < choices.length; i = i + 1) {
selectHTML += "<option value='" + choices[i] + "'>" + choices[i] + "</option>";}
selectHTML += "</select>";
Div.innerHTML = selectHTML;
document.getElementById(divName).appendChild(Div);
HTML HTML
<form class="new" method="post" action="phppage">
<fieldset id="products"> <legend> PRODUCTS </legend>
<select name="tProduct" id="cProduct">
<optgroup label="GALLON BAG">
<option>Product1</option>
<option>Product2</option>
<option>Product3</option>
</optgroup>
</select>
<label for="cQuantity">Quantity:<span style="color:red">*</span></label> <input type="number" name="tQuantity" id="cQuantity" placeholder="Qnt" min="0" max="9999" required/>
<div id="dynamicInput"></div>
<input type="button" value="New Product" onclick="addInput('dynamicInput');" />
My suggest is that you wrap the element you wanna clone in a div, and clone it when user click the button then put it under dynamicInput 我建议你将你想要克隆的元素包装在div中,并在用户单击按钮然后将其放在dynamicInput下时克隆它
function addInput(divName) { var copy = document.getElementById('forclone').cloneNode(true); document.getElementById(divName).appendChild(copy); } document.getElementById('button').onclick = function(){ addInput('dynamicInput'); }
<form class="new" method="post" action="phppage"> <fieldset id="products"> <legend> PRODUCTS </legend> <div id = 'forclone'> <select name="tProduct" id="cProduct"> <optgroup label="GALLON BAG"> <option>Product1</option> <option>Product2</option> <option>Product3</option> </optgroup> </select> <label for="cQuantity">Quantity:<span style="color:red">*</span></label> <input type="number" name="tQuantity" id="cQuantity" placeholder="Qnt" min="0" max="9999" required/> </div> <div id="dynamicInput"></div> <input type="button" value="New Product" id = 'button' />
i would like to say thank you to Z.better because i used the idea that he posted so solve this problem i only made some changes. 我想对Z.better说谢谢,因为我使用了他发布的想法解决了这个问题我只做了一些改动。 Follow below the script that worked with me. 按照下面与我一起工作的脚本。 I am using now. 我现在在用 I am sharing this because if someone have this doubt this is the solution. 我正在分享这个,因为如果有人怀疑这是解决方案。
<script>
function addInput(divName) {
var copy = document.getElementById('forclone').cloneNode(true);
document.getElementById(divName).appendChild(copy);
}
</script>
and 和
<form class="new" method="post" action="phppage">
<fieldset id="products"> <legend> PRODUCTS </legend>
<div id = 'forclone'>
<select name="tProduct" id="cProduct">
<optgroup label="GALLON BAG">
<option>Product1</option>
<option>Product2</option>
<option>Product3</option>
</optgroup>
</select>
<label for="cQuantity">Quantity:<span style="color:red">*</span></label> <input type="number" name="tQuantity" id="cQuantity" placeholder="Qnt" min="0" max="9999" required/>
</div>
<div id="dynamicInput"></div>
<input type="button" value="New Product" onclick="addInput('dynamicInput');" />
Thank you guys, this website is amazing. 谢谢你们,这个网站太棒了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.