将输入的值附加到匹配的“名称”标签div吗?

问题描述 投票:0回答:3

目标:将添加到输入的值与匹配的name="" tag 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>
javascript jquery html tags
3个回答
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>

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>

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;
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.