如何将文本框插入到从循环创建的div中 [英] How to insert a textbox into a div that is being created from a loop

查看:107
本文介绍了如何将文本框插入到从循环创建的div中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript jquery插件创建一个选择框和一个输入框。我在for循环中有它,它复制select和输入框的迭代次数。我想要做的是当它在循环中每次创建选择和输入框时,它会被抛入具有唯一ID的div中。



以下是我的解释,我目前正在感谢任何帮助。



我尝试过的事情:



  for (i =  0 ; i< Qty; i ++)
{
var newDiv = document 。 createElement(' div'); // 为每次迭代创建div
newDiv.id = ' d' + i; // 为每次迭代创建一个div id
newDiv.className = ' myClass'; // 创建了一个类
form.render(); // 此函数创建选择框和输入框
/ * 我被困在这里并不完全确定我是如何接受form.render()
并将其放入创建的div中。* /


}

解决方案

参考此样本



<!DOCTYPE html> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< style>
.myClass {
宽度:500px;
border:1px solid red;
填充:5px;
}

< / style>
< title>< / title>

< / head>
< body>
< script>
var数量= 5;
for(i = 0; i< Qty; i ++){
debugger
var newDiv = document.createElement('div');
document.body.appendChild(newDiv);
newDiv.id ='d'+ i;
newDiv.className ='myClass';
var input = document.createElement('input');
input.type =text;
input.id =txt+ i;
newDiv.appendChild(输入);


var select = document.createElement(select);
select.id =select+ i;
newDiv.appendChild(select);
var items = [option1,option2,option3];
for(var j = 0; j< items.length; j ++){
var option = document.createElement(option);
option.value = items [j];
option.text = items [j];
select.appendChild(option);
}

}
< / script>

< / body>
< / html>





演示 Plunker [ ^ ]


I am using a javascript jquery plugin that creates a select box and an input box. I have it inside a for loop which replicates the select and input box for the amount of times it iterates. What I am trying to do is while it is in the loop for each time the select and input box get created it gets thrown inside a div with a unique id of course.

Below is my explanation and where I am currently at appreciate any help.

What I have tried:

for(i=0; i<Qty; i++)
{
   var newDiv = document.createElement('div'); //created the div for each iteration
   newDiv.id = 'd'+ i; //created a div id for each iteration 
   newDiv.className = 'myClass'; //created a class
   form.render();//this function creates the select box and input box
   /*I am stuck here not exactly sure how I am suppose to take the form.render()
        and place it into the created div.*/

}

解决方案

refer this sample

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style>
        .myClass{
            width:500px;
            border:1px solid red;
            padding:5px;
        }
        
    </style>
    <title></title>
    
</head>
<body>
    <script>
        var Qty = 5;
        for (i = 0; i < Qty; i++) {
            debugger
            var newDiv = document.createElement('div');
            document.body.appendChild(newDiv);
            newDiv.id = 'd' + i;
            newDiv.className = 'myClass';
            var input = document.createElement('input');
            input.type = "text";
            input.id = "txt" + i;
            newDiv.appendChild(input);
             

            var select = document.createElement("select");
            select.id = "select" + i;
            newDiv.appendChild(select); 
            var items = ["option1", "option2", "option3"];
            for (var j = 0; j < items.length; j++) {
                var option = document.createElement("option");
                option.value = items[j];
                option.text = items[j];
                select.appendChild(option);
            }

        }
    </script>

</body>
</html>



Demo Plunker[^]


这篇关于如何将文本框插入到从循环创建的div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆