最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Best Way to Insert An Element With JavaScript - Stack Overflow

programmeradmin0浏览0评论

I have an HTML form which consists of several fieldsets (each has identical fields). I want users to be able to add additional fieldsets by clicking a button (jQuery). I can think of two approaches:

  1. Create a dummy hidden fieldset and clone it when the add button is clicked.

  2. Create a function that renders the fieldset's HTML from a string when the add button is clicked. (The function knows the fieldset as a long string.)

Are there any significant advantages or disadvantages to either appoach? Is there another way?

I don't love the idea of a dummy fieldset hanging around, even if it's hidden, but encoding the whole fieldset as a string seems a little unweildy, though I know it's pretty standard practice.

I have an HTML form which consists of several fieldsets (each has identical fields). I want users to be able to add additional fieldsets by clicking a button (jQuery). I can think of two approaches:

  1. Create a dummy hidden fieldset and clone it when the add button is clicked.

  2. Create a function that renders the fieldset's HTML from a string when the add button is clicked. (The function knows the fieldset as a long string.)

Are there any significant advantages or disadvantages to either appoach? Is there another way?

I don't love the idea of a dummy fieldset hanging around, even if it's hidden, but encoding the whole fieldset as a string seems a little unweildy, though I know it's pretty standard practice.

Share Improve this question edited May 23, 2017 at 12:21 CommunityBot 11 silver badge asked Jan 5, 2010 at 22:29 Alex ReisnerAlex Reisner 29.5k6 gold badges58 silver badges53 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 3

If even the fieldsets that are already present are identical to the one you'd like to insert, you can clone an existing fieldset and clear the inputs. Now you don't have to have the extra code in the HTML or Javascript :)

I have used the first approach (cloning) in the past, except that I actually removed the template block from the document using JavaScript so that its not "hanging" around. You can then just reuse the template block as needed by cloning the removed block and adding the clone back into the document.

The benefit with the template block approach is that you then keep the markup in the HTML document where it is easier to manage and keep updated than in encoded JavaScript somewhere.

Hope this helps.

You could create a fieldset from scratch when the button is pressed using DOM methods:

function createFieldset(legendText, prefix) {
    var fieldset = document.createElement("fieldset");
    var legend = fieldset.appendChild( document.createElement("legend") );
    legend.appendChild( document.createTextNode(legendText) );

    var input = document.createElement("input");
    input.type = "text";
    input.name = prefix + "_some_text_field";

    return fieldset;
}

var fieldset = createFieldset("Test fields", "test");
document.forms[0].appendChild(fieldset);

I admit this is verbose, but that's DOM for you.

You can clone the existing node you want to copy, change the appropriate attributes (like name) and then insert it into the page.

No reason to clone a dummy, just clone the existing one.

I think the issue is that I'd want to serialize the input name attributes. Otherwise i'd go with having a hidden fieldset and cloning that one, I'd then get the index of the inputs and write the name attributes.

You could load the new fieldset from an async source. This is probably overkill if the fieldset isn't plicated.

You said the fieldsets were all identical. If you load the page with one or more visible fieldsets, you could clone one of those, which would avoid having a dummy hidden fieldset in the page. You would have to clear any data that was in the fieldset before displaying it though.

From your options, I would pick #1 for ease of maintenance--if you decide later to adjust the formatting of the fieldsets, it's easier to change it in the HTML than in an unweildy JS string.

Since you are using jQuery and your fieldsets are identical you can use the handy clone() function..

so you could just do

$( 'form' ).append( $('fieldset:eq(0)').clone() );

or in another sequence

$('fieldset:eq(0)').clone().appendTo( 'form' );

发布评论

评论列表(0)

  1. 暂无评论