[英]Dynamically add/remove fields to a form in Rails3 using Jquery
有谁知道使用rails3中的JQuery在表单中动态添加字段的简单/便捷方法或最佳方法?
Regardless what server-side language you're using, you can just create elements with the jQuery constructor and append it to a form node. 无论您使用哪种服务器端语言,都可以使用jQuery构造函数创建元素并将其附加到表单节点。 For instance: 例如:
$('<input>', {
id: 'my_new_input_id',
}).appendTo($('#my_form_id'));
would create a new input
control and append it to a form with the id my_form_id
. 将创建一个新的input
控件,并将其附加到ID为my_form_id
的表单中。
edit 编辑
relating to your comment: To remove a dynamically created element, a good approach is to store a reference in a variable. 与您的评论有关:要删除动态创建的元素,一种好的方法是将引用存储在变量中。 Doing that you can call .remove()
or .detach()
later: 为此,您可以稍后再调用.remove()
或.detach()
:
var my_new_input_element = $('<input>', {
id: 'my_new_input_id',
}).appendTo($('#my_form_id'));
// ... lots of code
my_new_input_element.remove();
ref.: .appendTo , .remove() , .detach() 参考: .appendTo , .remove() ,.detach ()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.