如何使用克隆重复div但使新div使用唯一ID?

时间:2016-02-16 01:51:46

标签: javascript jquery html

我想在课堂上重复这个div"邀请"或当用户按下"邀请更多"按钮。我尝试使用克隆但它不会使用唯一ID重复div,我可以在以后访问这些字段。

在jquery代码中,我尝试克隆并找到一个输入字段,但我不确定我做错了什么。

我基本上是在尝试复制名称,号码,通知设置'当用户点击"邀请更多"按钮。



		$(".repeat").on('click', function() {
		  var num = $('.invite').length,
		    newNum = new Number(num + 1),
		    newElem = $('#entry' + num).clone().attr('id', 'entry' + newNum).fadeIn('slow');
		  newElem.find('.friendInput').attr('id', 'ID' + newNum + '_friend').attr('name', 'ID' + newNum + '_friend').val('');

		  $('#entry' + num).after(newElem);
		  $('#ID' + newNum + '_number').focus();

		});

<div class="invite">
  <input id="friendInput" name="friend" type="text" class="form-control form-white" placeholder="Name">
  <input id="numberInput" name="number" type="text" class="form-control form-white" maxlength="11 pattern=" \d{3}-?\d{3}-?\d{4} "
                    placeholder="(e.g.) 123-456-6789 ">
                    <div class="dropdown ">s
                        <button id="dLabel " class="form-control form-white dropdown " type="button " data-toggle="dropdown " aria-haspopup="true " aria-expanded="false ">
                            Notification Settings
                        </button>
                        <ul class="dropdown-menu animated fadeIn " role="menu " aria-labelledby="dLabel ">
                            <li class="animated lightSpeedIn "><a href="# ">Automatic</a></li>
                            <li class="animated lightSpeedIn "><a href="# ">Every 6 Hours</a></li>
                            <li class="animated lightSpeedIn "><a href="# ">Every 12 Hours</a></li>
                            <li class="animated lightSpeedIn "><a href="# ">Every 24 Hours</a></li>
                        </ul>
                    </div>

                    <button class = "repeat " id="add_more_friends " type="submit "  class="btn btn-submit ">Invite More</button>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如果您只是想更改克隆项目的ID:

...
    var tmpEl = $('#001').clone()
    tmpEl.attr('id', newID)
    $('#listOfThings').append(tmpEl)
...