简体   繁体   English

克隆内联表单字段并追加到父容器

[英]Clone inline form fields and append to parent container

I have the following form fields: 我有以下表单字段:

<div id="filename-url-container">
    <div class="form-inline form-group">
        <input name="filename[]" class="form-control" placeholder="Filename" type="text">
        <div class="input-group">
            <input name="url[]" class="form-control" placeholder="URL" type="text">
            <span class="input-group-btn">
                <button class="btn btn-default btn-add" type="button">
                    <span class="glyphicon glyphicon-plus"></span>
                </button>
            </span>
        </div>
    </div>
</div>

I want to grab the first child each time the button is pressed and append it to the bottom of the filename-url-container div without the values of the original cloned fields. 我想在每次按下按钮时抓住第一个孩子,并将其附加到filename-url-container div的底部, 而无需原始克隆字段的值。

I tried to get this to work but it's not appending correctly: 我试图使它工作,但未正确附加:

$('#filename-url-container').on('click', '.btn-add', function (e) {
    e.preventDefault();
    var formGroup= $('#filename-url-container :first-child');
    $('#filename-url-container').append(formGroup);
    console.log(controlForm);
});

You should .clone the elements before appending them - as written your code would simply take the existing elements and try to move them to exactly the same place. 您应该在添加元素之前先对其进行.clone -正如编写您的代码一样,代码将简单地使用现有元素,然后尝试将它们移到完全相同的位置。

You also need to constrain your selector, else it will pick every :first-child element within the container, not just the one that's the immediate child: 您还需要约束选择器,否则它将选择容器中的每个:first-child元素,而不仅仅是直接:first-child元素:

$('#filename-url-container').on('click', '.btn-add', function (e) {
    var formGroup= $('#filename-url-container > :first-child').clone(true);
    formGroup.find('input').val('');   // erase values
    $('#filename-url-container').append(formGroup);
});

demo at http://jsfiddle.net/alnitak/dvqgnga0/ 演示在http://jsfiddle.net/alnitak/dvqgnga0/

$('#filename-url-container').on('click', '.btn-add', function (e) {
    e.preventDefault();
    var formGroup= $('#filename-url-container div:first-child').html();
    $('#filename-url-container').append(formGroup);
    console.log(formGroup);
});

You need to append html, not object it self. 您需要附加html,而不是自己反对。 http://jsfiddle.net/npdh2v3L/ http://jsfiddle.net/npdh2v3L/

I don't think you are using :first-child correctly. 我认为您没有正确使用:first-child :first-child refers to the first child of the type to it's left. :first-child指类型左边的第一个孩子。 Other than that you just need to get rid of those values. 除此之外,您只需要摆脱这些值即可。 Additionally, I'm not seeing a need to Event.preventDefault() since you are not using a submit button. 此外,由于您没有使用提交按钮,因此我不需要Event.preventDefault()

$('#filename-url-container .btn-add').click(function(){
  var clone = $('#filename-url-container .form-group:first-child').clone(true);
  clone.find('input').val('');
  $('#filename-url-container').append(clone);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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