如何仅向每个div追加一次

时间:2019-07-08 18:29:25

标签: jquery append each

我有几个问题,用户将单击“是”或“否”按钮,并希望将其点击值附加到显示他们选择内容的空类中。

每次单击“是”或“否”时,该值都会附加到每个问题上,以便在底部显示这些值,即“是否否否是”。我希望它只显示该特定问题的值。

我到目前为止所拥有的-

$(function() {
var answer = $(".answer");
answer.each(function(index) {
$(this).on("click", function(){
  var userAnswer = $(this).attr("data-id");
  var displayAnswer = $(".display-answer");
  displayAnswer.each(function() {
      $(this).append(userAnswer);
    });
   });
  });
});

HTML

<div class="question-wrap">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores, ea maxime velit hic tenetur, animi. Similique dolorem modi quos eius in, unde, sapiente, non ipsa aliquam accusamus quis facilis enim.</p>
        <div class="answer-buttons">
            <div class="answer yes"><p>Yes</p></div>
            <div class="answer no"><p>No</p></div>
        </div>
        <div class="display-answer"></div>
    </div>

3 个答案:

答案 0 :(得分:1)

基于您的标记的简单示例:

$('.answer').on('click', function() {
    $(this).parent().next().html($(this).html());
    // `$(this).html()` gets the html of an element you clicked on
    // `$(this).parent().next()` - gets `.display-answer` element which 
    // is located `next` to the `parent` of the element you clicked
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
    <div class="question-wrap">
        <p>Question 1.</p>
        <div class="answer-buttons">
            <div class="answer yes"><p>Yes</p></div>
            <div class="answer no"><p>No</p></div>
        </div>
        <div class="display-answer"></div>
    </div>
    <div class="question-wrap">
        <p>Question 2.</p>
        <div class="answer-buttons">
            <div class="answer yes"><p>Yes</p></div>
            <div class="answer no"><p>No</p></div>
        </div>
        <div class="display-answer"></div>
    </div>
    <div class="question-wrap">
        <p>Question 3.</p>
        <div class="answer-buttons">
            <div class="answer yes"><p>Yes</p></div>
            <div class="answer no"><p>No</p></div>
        </div>
        <div class="display-answer"></div>
    </div>

答案 1 :(得分:1)

感谢您提供html。这意味着我们可以提供帮助!

只要结构保持完全相同,U_mulder的答案将绝对有效。我对使用索引的建议不起作用,因为每个问题有两个“答案”类。

在我在此处添加的代码段中,您将看到我首先使用.parents(".question-wrap").eq(0)来查找问题环绕div,然后使用.children(".display-answer").eq(0)来查找显示div。 .children()仅在元素的直接子元素中搜索。如果您需要向下看(与parents()相反),请使用.find()

您会在这里看到它的运行情况(使用.text()来获取值,因为没有data-id属性)

$(function() {
var answer = $(".answer");
answer.each(function(index) {
$(this).on("click", function(){
  var userAnswer = $(this).text();
  var displayAnswer = $(this).parents(".question-wrap").eq(0).children(".display-answer").eq(0);
  displayAnswer.text(userAnswer);
   });
  });
});
.question-wrap {
  flex: 1;
  min-width: 150px;
  display: inline-block;
  margin-left: 10px;
}

.answer {
  cursor: pointer;
}

.display-answer {
  height:20px;
  padding: 10px;
  border: 1px solid lightgreen
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="display: flex; flex-wrap: wrap">
<div class="question-wrap">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores, ea maxime velit hic tenetur, animi.</p>
    <div class="answer-buttons">
        <div class="answer yes"><p>Yes</p></div>
        <div class="answer no"><p>No</p></div>
    </div>
    <div class="display-answer"></div>
</div>
<div class="question-wrap">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores, ea maxime velit hic tenetur, animi.</p>
    <div class="answer-buttons">
        <div class="answer yes"><p>Yes</p></div>
        <div class="answer no"><p>No</p></div>
    </div>
    <div class="display-answer"></div>
</div>

<div class="question-wrap">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores, ea maxime velit hic tenetur, animi.</p>
    <div class="answer-buttons">
        <div class="answer yes"><p>Yes</p></div>
        <div class="answer no"><p>No</p></div>
    </div>
    <div class="display-answer"></div>
</div>

<div class="question-wrap">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores, ea maxime velit hic tenetur, animi.</p>
    <div class="answer-buttons">
        <div class="answer yes"><p>Yes</p></div>
        <div class="answer no"><p>No</p></div>
    </div>
    <div class="display-answer"></div>
</div>

<div class="question-wrap">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores, ea maxime velit hic tenetur, animi.</p>
    <div class="answer-buttons">
        <div class="answer yes"><p>Yes</p></div>
        <div class="answer no"><p>No</p></div>
    </div>
    <div class="display-answer"></div>
</div>
</div>

答案 2 :(得分:1)

这是一种获得您描述的结果的方法

    $(function(){
    	$(".answer").click(function(){ /*You can register the click handler easily in one step*/
      	var userAnswer = $(this).attr("data-id");
        $(this).parent().siblings(".display-answer").append(userAnswer); /*First, find the parent, then the "display-answer" siblings*/
      });
    
    });
p{
  font-family:sans-serif;
}

.answer{
  border:1px solid black;
  text-align:center;
  flex-grow:100;
  width:100px;
  display:inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="question-wrap">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores, ea maxime velit hic tenetur, animi. Similique dolorem modi quos eius in, unde, sapiente, non ipsa aliquam accusamus quis facilis enim.</p>
    <div class="answer-buttons">
      <div class="answer yes" data-id="yes"><p>Yes</p></div>
      <div class="answer no" data-id="no"><p>No</p></div>
    </div>
    <div class="display-answer"></div>
</div>
<div class="question-wrap">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores, ea maxime velit hic tenetur, animi. Similique dolorem modi quos eius in, unde, sapiente, non ipsa aliquam accusamus quis facilis enim.</p>
    <div class="answer-buttons">
      <div class="answer yes" data-id="yes"><p>Yes</p></div>
      <div class="answer no" data-id="no"><p>No</p></div>
    </div>
    <div class="display-answer"></div>
</div>