用javascript创建指定数目的文本框

来源:百度知道 编辑:UC知道 时间:2024/06/15 06:46:26
网络上都是查到时创建一个的,我想通过列表菜单选择创建指定数目的文本框,要怎么写代码呢?能给我一个完整的代码吗?
可不可以不是无限循环建立的那种啊?我要问的就是你这样的一个例子,呵呵,不过就是不想无限添加下去啊
第二个好像运行不正常哈,呵呵,我是想意思是先选择建4个文本框,结果就建了四个了,第一位的代码可以实现,但是,比如讲我先选了4个,并且4个已经建好了,但是却发现只需要3个文本框就够了,去选列表值中的3,但是这时候却建了4+3个,而不是我要的3个啊!

用onchange事件获取到值,然后创建元素就可以了,给你写了个例子

=====补充:给你修改了一下,在创建元素之前先清空一下就可以了

<script type="text/javascript">
function CreateInput(){
document.getElementById('addinput').innerHTML = "";
num = document.getElementById('select').value;
for(i=0;i<num;i++){
inform = document.getElementById('addinput');
newinput = document.createElement('input');
newinput.type = "text";
newinput.name = "text" + i;
newinput.id = "text" + i;
inform.appendChild(newinput);
addBr = document.createElement('<br />');
inform.appendChild(addBr);
}
}
</script>
<form id="form1" name="form1" method="post" action="">
<select name="select" id="select" onchange="CreateInput()">
<option value="1&