jquery clone form fields and increment id

前端 未结 4 2229
抹茶落季
抹茶落季 2020-11-27 03:37

I have a block of form elements which I would like to clone and increment their ID\'s using jQuery clone method. I have tried a number of examples but a lot of them only clo

4条回答
  •  青春惊慌失措
    2020-11-27 04:28

    Clone the main element, strip the id number from it. In the new element replace every instance of that id number in every element id you want incremented with the new id number.

    Ok, here's a quicky code here.

    Basically, this part is the most important:

    (parseInt(/test(\d+)/.exec($(this).attr('id'))[1], 10)+1
    

    It parses the current id (using RegEx to strip the number from the string) and increases it by 1. In your case instead of 'test', you should put 'clonedInput' and also not only increase the value of the main element id, but the three from the inside as well (category, subcategory and subsubcategory). This should be easy once you have the new id.

    Hope this helps. :)

提交回复
热议问题