jquery添加多个输入字段不起作用

时间:2016-02-19 07:34:59

标签: jquery html

当我点击按钮时想要添加多个输入字段,但它只适用于单个输入字段。想要那些输入字段内联。字段和删除按钮之间的空格。

add.js

$(document).ready(function(){
    var next = 1;
    $(".add-more").click(function(e){
        e.preventDefault();
        var addto = "#field" + next;
        var addRemove = "#field" + (next);
        next = next + 1;
        var newIn = '<input autcomplete="off" class="input form-control" id="field2' + next + '" name="field' + next + '" type="text">';
        var newIn1= '<select class="select" id="field1' + next +'" name="field">';
        var newInput = $(newIn+newIn1);
        var removeBtn = '<button id="remove' + (next - 1) + '" class="btn btn-danger remove-me" >-</button></div><div id="field">';
        var removeButton = $(removeBtn);
        $(addto).after(newInput);
        $(addRemove).after(removeButton);
        $("#field" + next).attr('data-source',$(addto).attr('data-source'));
        $("#count").val(next);  

            $('.remove-me').click(function(e){
                e.preventDefault();
                var fieldNum = this.id.charAt(this.id.length-1);
                var fieldID = "#field" + fieldNum;
                $(this).remove();
                $(fieldID).remove();
            });
    });



});

form.html

<form class="form-inline input-append" role="form">
    <button id="b1" class="btn add-more" type="button">Add Tax Component</button>
    <div  type="hidden" name="count" value="1">
        <div class="controls" id="profs">
            <div id="field" class="form-group col-md-12">
                 <select class="select col-md-4" id="field1" name="prof2" hidden>
                      <option>select Tax</option>
                 </select>
                 <input autocomplete="off" class="input col-md-8" id="field2" name="prof1" type="text" placeholder="Type something"  hidden data-items="8"/>
            </div>
        </div>
    </div>
</form>

3 个答案:

答案 0 :(得分:1)

您将在第一个事件后添加到未存在的ID。因为&#34; #field2&#34;你的HTML中不存在。

看看这个,我编辑了它:

add.js

$(document).ready(function(){
        var next =1;
        $(".add-more").click(function(e){
            e.preventDefault();
            console.log(next);
            var addto = "#field" + next;
            var addRemove = "#field" + (next);
            next = next + 1;
            var newIn = '<input autcomplete="off" class="input form-control" id="field2' + next + '" name="field' + next + '" type="text">';
            var newIn1= '<select class="select" id="field' + next +'" name="field">';
            var newInput = $(newIn+newIn1);
            var removeBtn = '<button id="remove' + (next - 1) + '" class="btn btn-danger remove-me" >-</button></div><div id="field">';
            // var removeButton = $(removeBtn);
            $(addto).after(newInput);
            $(addRemove).after(removeBtn);
            $("#field" + next).attr('data-source',$(addto).attr('data-source'));
            $("input[name=count]").val(next);  

        });


        $('body').on("click", ".remove-me", function(e){
            e.preventDefault();
            var fieldNum = this.id.charAt(this.id.length-1);
            var fieldID = "#field2" + fieldNum;
            var selectfieldID = "#field" + fieldNum;
            console.log(fieldNum);
            console.log(fieldID);
            console.log(selectfieldID);
            $(this).remove();
            $(fieldID).remove();
            $(selectfieldID).remove();
        });

    });

而且,不要在事件监听器中定义事件监听器,它将创建重复项,您的1次单击将具有多个事件。尝试使用我在编辑过的代码上使用的.on()。

答案 1 :(得分:0)

您应该根据其他操作数据定义一些标准,或多少输入,例如

var howManyInputs = 15;
$('#target').click(function () {
    for (var i = 0; i < howManyInputs; i++) {
        // add your inputs
    }
    // add your remove button
});

答案 2 :(得分:0)

我看了一下你的代码,并用它做了这个。我没有在每次添加元素时创建元素,而是冒昧地重写代码,以便克隆初始元素(模板)并将其附加到文档中。

请看下面的代码段。这可能是你要求的?

&#13;
&#13;
$(".template:first").hide(); //hide template

/* Add new item based on hidden template */
$(".add-more").click(function() {
  var newItem = $(".template:first").clone();
  newItem.find("select").attr("id", "field" + ($(".template").length + 2)); //rewrite id's to avoid duplicates
  newItem.find("input").attr("id", "field" + ($(".template").length + 2)); //rewrite id's to avoid duplicates
  newItem.show(); //show clone of template
  $(".template:last").after(newItem);
  bindRemove();
});

/* Bind remove function to last added button*/
function bindRemove() {
  $(".remove:last").click(function(e) {
    if ($(".remove").length > 1)
      $(this).parents(".template").remove();
  });
}

/* Execute bind-function at startup */
bindRemove();
&#13;
.template {
  border: 2px solid black;
  height: 100px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-inline input-append" role="form">
  <button id="b1" class="btn add-more" type="button">Add Tax Component</button>
  <div class="template">
    <div class="controls" id="profs">
      <div id="field" class="form-group col-md-12">
        <select class="select col-md-4" id="field1" name="prof2">
          <option>select Tax</option>
        </select>
        <br/>
        <br/>
        <input autocomplete="off" class="input col-md-8" id="field2" name="prof1" type="text" placeholder="Type something" data-items="8" />
        <button class="remove" type="button">
          X
        </button>
      </div>
    </div>
  </div>
</form>
&#13;
&#13;
&#13;