使用匹配的“名称”属性将输入的值追加到div

时间:2019-07-16 12:13:31

标签: javascript jquery

目标:将添加到输入中的值与匹配的name="" value附加到div。

问题:当存在一个div和一个输入,甚至两个div和一个输入时,我的代码(如下)工作正常,但是当我添加第二个输入时,该代码就不会执行,因为这意味着

$('.example-default-value').each(function() {
  var default_value = this.value;
  $(this).focus(function() {
    if (this.value == default_value) {
      this.value = '';
    }
  });
  $(this).blur(function() {
    if (this.value == '') {
      this.value = default_value;
    }
  });
});

function example_append() {

  if ($('.example-textarea').attr('name') === $('.example').attr('name')) {
    $('.example').append($('.example-textarea').val());
  }

};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="example" name="add" style="border: 1px solid rgb(204, 204, 204);float: left; margin: 5px 0pt; padding: 5px;">Example div</div>

<div class="example" name="add1" style="border: 1px solid rgb(204, 204, 204);float: left; margin: 5px 0pt; padding: 5px;">Example div</div>

<form>
  <div><textarea class="example-default-value example-textarea" name="add1" style="width: 400px; height: 50px;">Type some text in here to be appended</textarea></div>

  <div><textarea class="example-default-value example-textarea" name="add" style="width: 400px; height: 50px;">Type some text in here to be appended</textarea></div>

  <div><input type="button" value="Append" onclick="example_append()" /></div>

</form>

3 个答案:

答案 0 :(得分:1)

您应该使用Attribute value Selector定位所需的元素,并获取其值。

function example_append() {
  $('.example').append(function() {
    return $('.example-textarea[name=' + $(this).attr('name') + ']').val()
  });
}

function example_append() {
  $('.example').append(function() {
    return $('.example-textarea[name=' + $(this).attr('name') + ']').val()
  });
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="example" name="add" style="border: 1px solid rgb(204, 204, 204);float: left; margin: 5px 0pt; padding: 5px;">Example div</div>

<div class="example" name="add1" style="border: 1px solid rgb(204, 204, 204);float: left; margin: 5px 0pt; padding: 5px;">Example div</div>

<form>
  <div><textarea class="example-default-value example-textarea" name="add1" style="width: 400px; height: 50px;">Type some text in here to be appended</textarea></div>

  <div><textarea class="example-default-value example-textarea" name="add" style="width: 400px; height: 50px;">Type some text in here to be appended</textarea></div>

  <div><input type="button" value="Append" onclick="example_append()" /></div>

</form>

答案 1 :(得分:0)

textareas不止一个,因此您需要应用循环。

function example_append() {
  const divs = $('div');
  const inputs = $('.example-textarea');

  inputs.each(function() {
    const that = $(this);
    divs.each(function(i, div) {
      if ($(div).attr('name') == that.attr('name')) {
        $(div).text(that.val());
      }
    });

  });
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="example" name="add" style="border: 1px solid rgb(204, 204, 204);float: left; margin: 5px 0pt; padding: 5px;">Example div</div>
<div class="example" name="add1" style="border: 1px solid rgb(204, 204, 204);float: left; margin: 5px 0pt; padding: 5px;">Example div</div>
<form>
  <div><textarea class="example-default-value example-textarea" name="add1" style="width: 400px; height: 50px;">Type some text in here to be appended</textarea></div>
  <div><textarea class="example-default-value example-textarea" name="add" style="width: 400px; height: 50px;">Type some text in here to be appended</textarea></div>
  <div><input type="button" value="Append" onclick="example_append()" /></div>
</form>

答案 2 :(得分:0)

问题是您的textareas没有唯一的名称,因此此代码不起作用(找到了多个元素):

if ($('.example-textarea').attr('name') === $('.example').attr('name')){
    $('.example').append($('.example-textarea').val());
}

这是一个普通的JS(即没有JQuery)示例,您可以用来获取所需的结果:

<div id="add1"></div>
<div id="add"></div>

<textarea name="add1"></textarea>
<textarea name="add"></textarea>

<button>append</button>
const btn = document.querySelector('button');
btn.addEventListener('click', () => {
    document.querySelectorAll('textarea')
  .forEach(t => {
    let name = t.getAttribute('name');
    let value = t.value;
    document.getElementById(name).innerHTML = value;
  });
});