使用jquery附加和删除多个元素

时间:2018-04-18 01:17:39

标签: 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--;
  });


});
&#13;
<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>
&#13;
&#13;
&#13;

您可以输入您的姓名,它会自动显示在下方。您可以添加更多&#34;成员&#34;并键入他们的名字,同样会发生。它工作正常。

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

&#13;
&#13;
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--;
  });


});
&#13;
.block {
  display: block;
}

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

span.add,
span.remove {
  display: inline-block;
  cursor: pointer;
  text-decoration: underline;
}
&#13;
<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>
&#13;
&#13;
&#13;

在最后一个代码中,第一个&#34;输入组&#34;工作得很好。我的问题来自第二个(&#34;第一个添加的成员&#34;):它不会在一行中创建短语(我尝试使用display:在div内部内联但是没有工作)。

如果你添加第三组输入(&#34;添加第三个成员&#34;)它会变得更糟(你会看到)

我需要所有其他添加的&#34;输入组&#34;完全像第一个一样工作。

1 个答案:

答案 0 :(得分:2)

您的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 vars。您应该能够使用单个变量实现此目标,每次添加新组时,您只需增加该变量。它还可以创建无限制的组,您也可以通过在创建组时检查增量变量来限制它。