动态添加/删除多个输入字段
我有一个表单,其中多个字段可能有多个输入,即
- 您的名字
- 您喜欢的歌曲:[添加更多!]
- 您喜欢的电影:[添加更多!]
- 显示您喜欢:[添加更多!]
我还是个新人,一直在四处寻找教程。似乎其中相当一部分倾向于只关注一个字段,并且倾向于在 javascript 中包含输入标记,这使得我似乎必须为每个不同的输入字段复制并粘贴脚本。即这个: http://muiomuio.com/web-design/add-remove-items -with-jquery
$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').live('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
i++;
return false;
});
$('#remScnt').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
有没有办法简化这个?非常感谢您的帮助!
I have a form where several fields may have multiple inputs, i.e.
- Your Name
- Songs you like: [add more!]
- Movies you like: [add more!]
- shows you like: [add more!]
I'm still new and have been looking around for tutorials. It seems like quite a bit of them tend to focus on just one field and tend to have input markup in the javascript, which makes it seem like I'd have to copy and paste the script for each different input field. i.e. this one:
http://muiomuio.com/web-design/add-remove-items-with-jquery
$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').live('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
i++;
return false;
});
$('#remScnt').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
Is there a way to streamline this? Thank you so much for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这个 jsFiddle。我编写了一个用于添加文件的脚本,但删除它们的脚本类似。
玩得开心
K
Have a look at this jsFiddle. I wrote a script for adding the fileds but it would be similar for removing them.
Hve fun
K