javascript - 使用 jquery 添加和删除多个元素

标签 javascript jquery append

我有这个代码

var name1 = document.getElementById('1');
name1.addEventListener('input', function() {
  var result = document.querySelector('span.one');
  result.innerHTML = this.value;
});


$(document).ready(function() {
  var max_fields = 20; //maximum input boxes allowed
  var wrapper = $(".input_fields_wrap"); //Fields wrapper
  var add_button = $(".add_field_button"); //Add button ID

  var x = 1; //initlal text box count
  $(add_button).click(function(e) { //on add input button click
    e.preventDefault();
    if (x < max_fields) { //max input box allowed
      x++; //text box increment
      $(wrapper).append('<div class ="' + x + '" ><br><input type="text" class= "' + x + '" name="mytext[]"/><a href="#" class="remove_field">Remove</a><br></div>'); //add input box
      $('.hello:last').after('<div class="hello" id = "' + x + '"   >Hello, <span class="name"></span><br><br></div>');

      $('input').on('input', function(e) {

        divtoappend = $(this).attr('class');
        var val = "";
        var val = $(this).val();
        var sel = "#" + divtoappend + " span";
        $(sel).text('');
        $(sel).append(val);
      });

    }
  });

  $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
    e.preventDefault();
    var rem = $(this).parents('div').attr('class');
    $('#' + rem).remove();
    $(this).parent('div').remove();
    x--;
  });


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input_fields_wrap">
  <button class="add_field_button">Add member</button>
  <br><br>
  <div><input type="text" id="1" name="mytext[]"></div>
</div>
<br>
<div class="hello" id="1">

  Hello, <span class="one"></span><br><br>

</div>

您可以输入您的名字,它会自动出现在下面。您可以添加更多“成员”并输入他们的名字,同样的情况也会发生。效果很好。

现在,我尝试为每组添加 3 个输入,并使用这 3 个输入创建一个短语。像这样http://jsfiddle.net/JBxnQ/165/

var name1 = document.getElementById('name');
name1.addEventListener('input', function() {
  var result = document.querySelector('span.one');
  console.log(this.value);
  result.innerHTML = this.value;
});

var name1 = document.getElementById('city');
name1.addEventListener('input', function() {
  var result = document.querySelector('span.two');
  console.log(this.value);
  result.innerHTML = this.value;
});

var name1 = document.getElementById('age');
name1.addEventListener('input', function() {
  var result = document.querySelector('span.three');
  console.log(this.value);
  result.innerHTML = this.value;
});



$(document).ready(function() {
  var max_fields = 5; //maximum input boxes allowed
  var wrapper = $(".input_fields_wrap"); //Fields wrapper
  var add_button = $(".add_field_button"); //Add button ID

  var x = 1; //initlal text box count
  var y = 2;
  var z = 3;
  $(add_button).click(function(e) { //on add input button click
    e.preventDefault();
    if (x, y, z < max_fields) { //max input box allowed
      x++; //text box increment
      y++;
      z++;
      $(wrapper).append('<div class ="' + x + '" ><label>Name</label><input type="text" class= "' + x + '" name="mytext[]"/><a href="#" class="remove_field">Remove</a><br></div><div class ="' + y + '" ><label>City</label><input type="text" class= "' + y + '" name="mytext[]"/> <br> </div> <div class ="' + z + '" ><label>Age</label><input type="text" class= "' + z + '" name="mytext[]"/> <br> </div>'); //add input box
      $('.hello:last').append('<div class="hello" id = "' + x + '"   >Hello, <span class="one"></span></div><div class="hello" id = "' + y + '"   >, from <span class="two"></span></div><div class="hello" id = "' + z + '"   >. Your age is: <span class="three"></span><br><br></div>');

      $('input').on('input', function(e) {

        divtoappend = $(this).attr('class');
        var val = "";
        var val = $(this).val();
        var sel = "#" + divtoappend + " span";
        $(sel).text('');
        $(sel).append(val);
      });

    }
  });

  $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
    e.preventDefault();
    var rem = $(this).parents('div').attr('class');
    $('#' + rem).remove();
    $(this).parent('div').remove();
    x--;
    y--;
    z--;
  });


});
.block {
  display: block;
}

input {
  width: 50%;
  display: inline-block;
  margin: 4px;
}

span.add,
span.remove {
  display: inline-block;
  cursor: pointer;
  text-decoration: underline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div class="input_fields_wrap">
  <button class="add_field_button">Add More Fields</button>
  <br><br>
  <label>Name</label><input type="text" id="name" name="mytext[]"> <br>
  <label>City</label><input type="text" id="city" name="mytext[]"> <br>
  <label>Age</label><input type="text" id="age" name="mytext[]">
</div>
<br>
<div class="hello" id="1">

  Hello, <span class="one"></span>, from <span class="two"></span>. Your age is: <span class="three"></span><br><br>

</div>

在最后的代码中,第一个“输入组”工作得很好。我的问题来自第二个(“第一个添加的成员”):它不会将短语放在一行中(我尝试在 div 内使用 display:inline 但不起作用)。

如果您添加第三组输入(“添加第三个成员”),情况会变得更糟(您会看到)

我需要所有其他添加的“输入组”与第一个输入完全一样。

最佳答案

你的 JS 正在生成一个与 HTML 中的初始 DOM 不同的 DOM。

HTML:

<div class="hello" id="1">
  Hello, <span class="one"></span>, from <span class="two"></span>. Your age is: 
  <span class="three"></span><br><br>
</div>

但是你的 JS 正在创建这个:

<div class="hello" id = "x"   >
  Hello, 
  <span class="one"></span>
</div>
<div class="hello" id = "y"   >
  , from <span class="two"></span>
</div>
<div class="hello" id = "z"   >
  . Your age is: 
  <span class="three"></span><br><br>
</div>

您需要删除所有类并只拥有相同的 DOM。

$('.hello:last').append('<div class="hello" id = "' + i + '"   >Hello, <span class="one"></span>, from <span class="two"></span>. Your age is: <span class="three"></span><br><br></div>');

我也有点困惑为什么你有 x y z 变量。您应该能够使用单个变量来实现此目的,只需在每次添加新组时增加该变量即可。它还可以创建无限的组,您也可以通过在创建组时检查递增的 var 来限制它。

关于javascript - 使用 jquery 添加和删除多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49889683/

相关文章:

javascript - 验证不适用于动态添加的字段?

javascript - 如何在移动设备上按下 Web Audio 元素时立即发出声音?

javascript - jQuery 通过 attr 添加 id 不工作

jQuery UI 焦点窃取

javascript - 无法获取表中克隆行的日期选择器

javascript - jQuery 追加 html

javascript - 为什么我的 Prop 在我需要的时候不能在我的组件中使用?

javascript - if (json.RowKey != json.NewRowKey) 有什么问题

javascript - 为什么 jQuery 数据不能与追加一起使用?

Angular2-http ://localhost:4200/being appended with api call why?