admin管理员组文章数量:1327849
Im having trouble creating multiple input texts with javascript.
My point is create a new input text everytime the input before is pleted. (parent?)
Ive some code for boboxs, but this time I need just input text box.
How can I do that ?
I've found this code:
<script type="text/javascript">
function addInput()
{
var x = document.getElementById("inputs");
x.innerHTML += "<input type=\"text\" />";
}
</script>
<input type="button" onmousedown="addInput();" />
<div id="inputs"></div>
But for my problem button is obsolete.
I think my event trigger will be something arround this "when user click in an input text box and it is != blank it creates a new one".
I migth need some ID to identify every input text box.
Cheers.
Im having trouble creating multiple input texts with javascript.
My point is create a new input text everytime the input before is pleted. (parent?)
Ive some code for boboxs, but this time I need just input text box.
How can I do that ?
I've found this code:
<script type="text/javascript">
function addInput()
{
var x = document.getElementById("inputs");
x.innerHTML += "<input type=\"text\" />";
}
</script>
<input type="button" onmousedown="addInput();" />
<div id="inputs"></div>
But for my problem button is obsolete.
I think my event trigger will be something arround this "when user click in an input text box and it is != blank it creates a new one".
I migth need some ID to identify every input text box.
Cheers.
Share Improve this question asked Dec 17, 2012 at 11:14 user1148875user1148875 4594 gold badges11 silver badges25 bronze badges 4- What do you mean by pleted? As soon as it has a single character in it? – Majid Fouladpour Commented Dec 17, 2012 at 11:19
-
i thing you should first make sure what you want to achieve. As i understood, you want to begin with a
input
-textbox and create another one, once the user clicks in it after he typed something in it. This is really strange behavior, why would you want that? And yes: of coursem, you would need anid
. – nozzleman Commented Dec 17, 2012 at 11:26 - @nozzleman, yes thats i want to achieve. why you say thats strange? – user1148875 Commented Dec 17, 2012 at 11:34
- @nozzleman, This will be to user selected a range of numbers by using input-textboxs – user1148875 Commented Dec 17, 2012 at 11:35
3 Answers
Reset to default 2JSBIn Demo
Guess this helps:
<div id="myDiv">
<input type="text" id="txt_1" onkeydown="newTextBox(this)" />
</div>
<script type="text/javascript">
function newTextBox(element){
if(!element.value){
element.parentNode.removeChild( element.nextElementSibling);
return;
}
else if(element.nextElementSibling)
return;
var newTxt = element.cloneNode();
newTxt.id = 'txt_'+( parseInt( element.id.substring(element.id.indexOf('_')+1)) + 1);
newTxt.value='';
element.parentNode.appendChild(newTxt);
}
</script>
HTML code:
<div id="inputcontainer">
<input type="text" name="input0" id="input0" onkeyup="addInput();" />
</div>
And Javascript:
var currentindex = 0;
function addInput(){
var lastinput = document.getElementById('input'+currentindex);
if(lastinput.value != ''){
var container = document.getElementById('inputcontainer');
var newinput = document.createElement('input');
currentindex++;
newinput.type = "text";
newinput.name = 'input'+currentindex;
newinput.id = 'input'+currentindex;
newinput.onkeyup = addInput;
container.appendChild(newinput);
}
}
This will add a new input to the list only when the last input is not empty.
http://jsfiddle/HJbgS/
Have a look at the onchange
event on your text input field. You can use it, like you use onmousedown
on your button.
See http://www.w3schools./jsref/event_onchange.asp for an example.
In your addInput()
function you should then check if the input of the previous textfield is != "".
本文标签: Multiple dynamic input text javascriptStack Overflow
版权声明:本文标题:Multiple dynamic input text javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742219115a2435115.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论