[英]HTML Add value in textbox
I want to add value in text box. 我想在文本框中添加值。 A text box is already created with default value equal 10, then a button use a function to adding new text box with limit 4. therefore, i want to add value continuously from text box 1 to text box 4 已经创建了一个默认值为10的文本框,然后一个按钮使用一个函数来添加限制为4的新文本框。因此,我想从文本框1到文本框4连续添加值。
Example: 例:
Text box 1 value = 10 文本框1值= 10
Text box 2 value = 20 文本框2的值= 20
Text box 3 value = 30 文本框3值= 30
Text box 4 value = 40 文本框4值= 40
How can i do this? 我怎样才能做到这一点?
<script type="text/javascript">
//Adding New Line Item Function
var i = 1;
function AddNew(){
if (i <=3){ //if you don't want limit, you remove IF condition
i++;
var div= document.createElement('div');
div.innerHTML = '<input type="text" name="lineitem_'+i+'" maxlength="2" size="2"><input type="button" onclick="removeKid(this)" value="-">';
document.getElementById('addingitem').appendChild(div);
}
}
</script>
Use like this.... 像这样使用...
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="textboxes">
Text box 1 <input type="text" value="10" />
</div>
<button onclick="AddNew()" >Add</button>
<script type="text/javascript">
//Adding New Line Item Function
var i = 1;
function AddNew(){
var div= document.getElementById('textboxes');
if (i <4){
i++;
div.innerHTML = div.innerHTML+'<br>Text box '+i+' <input type="text" value="'+i*10+'" />';
}
}
</script>
</body>
</html>
You want to add value continuously from text box 1 to text box 4 ,so while
loop is more better here 您希望从文本框1到文本框4连续增加值,因此while
循环在此处效果更好
var i = 1;
function AddNew(){
while(i < 4 ){
i++;
var div = document.getElementById("addingitem");
div.innerHTML += '<br>Text box '+i+' <input type="text" name="lineitem_'+i+'" maxlength="2" size="2" value="'+i*10+'"><input type="button" onclick="removeKid(this)" value="-">';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.