输入数字以添加更多输入 JQuery
我想要一个表单输入,可以输入一个数字,然后单击旁边的按钮,这样它就会“添加”以前的输入数量,并每个输入都有自己的名称。
- 输入数字
- 单击“执行”按钮或
- 同一页面上输入数量的任何输出(创建新输入)
我尝试过一次只提供“添加一个”功能:
$(function() {
var i = $('input').size() + 1; // check how many input exists on the document and add 1 for the add command to work
$('a#add').click(function() { // when you click the add link
$('<p><input type="text" value="' + i + '" /></p>').appendTo('body'); // append (add) a new input to the document.
// if you have the input inside a form, change body to form in the appendTo
i++; //after the click i will be i = 3 if you click again i will be i = 4
});
$('a#remove').click(function() { // similar to the previous, when you click remove link
if(i > 1) { // if you have at least 1 input on the form
$('input:last').remove(); //remove the last input
i--; //deduct 1 from i so if i = 3, after i--, i will be i = 2
}
});
$('a.reset').click(function() {
while(i > 2) { // while you have more than 1 input on the page
$('input:last').remove(); // remove inputs
i--;
}
});
});
<a href="#" id="add">Add</a>
<a href="#" id="remove">Remove</a>
<input type="text" value="1" />
I want to have a form input that one would enter a number and click the button next to it so it would "Add" the former number of Inputs with each its own name.
- Enter Number
- Click Go Button or whatever
- Outputs on same page that number of inputs (creates new inputs)
I have tried this which only gives an ADD one at a time feature:
$(function() {
var i = $('input').size() + 1; // check how many input exists on the document and add 1 for the add command to work
$('a#add').click(function() { // when you click the add link
$('<p><input type="text" value="' + i + '" /></p>').appendTo('body'); // append (add) a new input to the document.
// if you have the input inside a form, change body to form in the appendTo
i++; //after the click i will be i = 3 if you click again i will be i = 4
});
$('a#remove').click(function() { // similar to the previous, when you click remove link
if(i > 1) { // if you have at least 1 input on the form
$('input:last').remove(); //remove the last input
i--; //deduct 1 from i so if i = 3, after i--, i will be i = 2
}
});
$('a.reset').click(function() {
while(i > 2) { // while you have more than 1 input on the page
$('input:last').remove(); // remove inputs
i--;
}
});
});
<a href="#" id="add">Add</a>
<a href="#" id="remove">Remove</a>
<input type="text" value="1" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西吗?
Something like this?
您可以这样做(根据您的需要调整此代码):
基本上,当您单击“Go”按钮时,您想要获取输入的值,并循环多次。每次迭代,创建一个新的
input
元素并将其附加到原始表单。You could do something like this (adapt this code to your needs):
Basically, when you click the 'Go' button, you want to take the inputted value, and loop that many times. Each iteration, create a new
input
element and append it to the original form.有点晚了,但是一个不错的选择是使用 jQuery 模板
$.tmpl
(假设您不反对使用插件)jsfiddle。
Sort of late to the party, however a good option would be to use jQuery templates
$.tmpl
(assuming you are not opposed to using a plugin)Code example on jsfiddle.