简体   繁体   中英

Dynamic form elements

I need a form that allows users to dynamically add new fields. In my case, one line contains a label and a textarea, and clicking on the plus sign would add a new line with label and textarea.

I found the plugin http://plugins.jquery.com/project/dynamicField , which does exactly what I want, except for one thing: for each added line, I need the label to contain the number of the line.

Any hints on how this could be done?

Best would probably be to add an event in the plugin like 'AfterAdd' that I can subscribe to and set the label or something?

EDIT: does anyone know a plugin that does this kind of 'dynamic form' thing, where you can add, insert and delete rows?

Thanks, L

I don't understand why you'd use a plugin for this. It's only a couple of lines of code. Something like:

$('#add').click(function() {
    var $container =  $('#container');
    var html ='<label>' 
                + ($container.find('textarea').length + 1) 
                + '</label><textarea></textarea><br />';
    $container.append(html);
});

example here

You can try using jquery templates for that purpose. Something quite clean and great for dynamic DOM generation/insertion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM