简体   繁体   中英

Clone element input javascript with null value

$(document).ready(function() {
        $('#btnAdd').click(function() {
            var num     = $('.clonedInput').length;
            var newNum  = new Number(num + 1);

            var newElem = $('#input' + num).cloneNode(true).attr('id', 'input' + newNum);

            newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
            $('#input' + num).after(newElem);
            $('#btnDel').attr('disabled','');

            if (newNum == 5)
                $('#btnAdd').attr('disabled','disabled');
        });

I cloned the textbox but the code does not work, how do I resolve this?

Try

var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

As per documentFragment.cloneNode(true) doesn't clone jQuery data

Update: perhaps you want to clone num-1 since num is the new field that does not yet exist

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