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

Kamalkrishna Sahu

我想在单击按钮时添加多个输入字段,但仅适用于单个输入字段。想要这些输入字段内联。在字段和删除按钮之间留有空格。

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>
约雷克斯

我看了看您的代码,并从中得出了这一点。我不必每次添加都创建一个元素,而是自由地重写代码,以便它克隆一个初始元素(模板)并将其附加到您的文档中。

看看下面的代码片段。这可能是您要的吗?

$(".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();
.template {
  border: 2px solid black;
  height: 100px;
}
<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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用jQuery添加多个输入不起作用

来自分类Dev

我正在尝试动态添加多个tagit字段,但对我不起作用

来自分类Dev

jQuery添加多个具有相同名称的CSS属性不起作用

来自分类Dev

为什么添加多个渐变不起作用?

来自分类Dev

添加多个列大小时,缀不起作用

来自分类Dev

ReactJS动态添加多个输入字段

来自分类Dev

反应多个输入字段onChange不起作用

来自分类Dev

添加多个动态行后,添加动态行的值不起作用

来自分类Dev

动态添加的输入的产品/添加不起作用 jquery

来自分类Dev

jQuery UI子菜单中的输入字段不起作用

来自分类Dev

添加多个侦听器,它们将侦听不同的RabbitMQ队列不起作用

来自分类Dev

输入字段不起作用的JavaScript onClick

来自分类Dev

动态输入字段不起作用

来自分类Dev

如何在输入字段中添加多个值

来自分类Dev

多个文件输入html不起作用

来自分类Dev

在JavaScript中以编程方式将onclick事件添加到输入字段不起作用

来自分类Dev

表单字段值的总和在jquery添加的字段中不起作用

来自分类Dev

表单字段值的总和在jquery添加的字段中不起作用

来自分类Dev

jQuery-在多个字段上应用多个事件-不起作用

来自分类Dev

jQuery textarea(+字段)焦点不起作用

来自分类Dev

jQuery禁用输入不起作用

来自分类Dev

输入在jQuery mobile中不起作用

来自分类Dev

jQuery Focusout输入不起作用

来自分类Dev

jQuery令牌输入-allowCustomEntry不起作用

来自分类Dev

jQuery的点击输入[类型=“]不起作用

来自分类Dev

jQuery禁用搜索输入不起作用

来自分类Dev

jQuery检查单选输入不起作用

来自分类Dev

使用jQuery .val()设置输入字段值时,引导程序验证器不起作用

来自分类Dev

jQuery:验证错误后,我的输入掩码功能在字段上不起作用

Related 相关文章

  1. 1

    使用jQuery添加多个输入不起作用

  2. 2

    我正在尝试动态添加多个tagit字段,但对我不起作用

  3. 3

    jQuery添加多个具有相同名称的CSS属性不起作用

  4. 4

    为什么添加多个渐变不起作用?

  5. 5

    添加多个列大小时,缀不起作用

  6. 6

    ReactJS动态添加多个输入字段

  7. 7

    反应多个输入字段onChange不起作用

  8. 8

    添加多个动态行后,添加动态行的值不起作用

  9. 9

    动态添加的输入的产品/添加不起作用 jquery

  10. 10

    jQuery UI子菜单中的输入字段不起作用

  11. 11

    添加多个侦听器,它们将侦听不同的RabbitMQ队列不起作用

  12. 12

    输入字段不起作用的JavaScript onClick

  13. 13

    动态输入字段不起作用

  14. 14

    如何在输入字段中添加多个值

  15. 15

    多个文件输入html不起作用

  16. 16

    在JavaScript中以编程方式将onclick事件添加到输入字段不起作用

  17. 17

    表单字段值的总和在jquery添加的字段中不起作用

  18. 18

    表单字段值的总和在jquery添加的字段中不起作用

  19. 19

    jQuery-在多个字段上应用多个事件-不起作用

  20. 20

    jQuery textarea(+字段)焦点不起作用

  21. 21

    jQuery禁用输入不起作用

  22. 22

    输入在jQuery mobile中不起作用

  23. 23

    jQuery Focusout输入不起作用

  24. 24

    jQuery令牌输入-allowCustomEntry不起作用

  25. 25

    jQuery的点击输入[类型=“]不起作用

  26. 26

    jQuery禁用搜索输入不起作用

  27. 27

    jQuery检查单选输入不起作用

  28. 28

    使用jQuery .val()设置输入字段值时,引导程序验证器不起作用

  29. 29

    jQuery:验证错误后,我的输入掩码功能在字段上不起作用

热门标签

归档