简体   繁体   English

jQuery在单击和替换时附加值

[英]jQuery append value on click and replace

I'm trying to append the button text to the span but have it only show the value from the last clicked button under each .item . 我正在尝试将button文本追加到span但只显示每个.item下最后单击的button的值。

So whatever the button text is that is clicked, append it to the span and have that value replaced by the next pressed button text, and so on. 因此,无论单击什么button文本,都appendappendspan并用下一个按下的button文本替换该值,依此类推。

 $(".list").on("click", ".button", function() { var x = $(this).closest(".item"); var y = $(".button").text(); $('#value').append('(' + y + ')'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="list"> <div class="item"> <div>Item 1</div> <button class="button">50</button> </div> <div class="item"> <div>Item 2</div> <button class="button">30</button> </div> <div class="item"> <div>Item 3</div> <button class="button">70</button> </div> </div> <span id="value">Total:</span> 

I would structure your html a little like this and then target the span and update it using .text() . 我会像这样构造您的html,然后指定范围并使用.text()更新它。 I used the <span> inside of the <p> tag like this because if you try and update the whole span tag, it'll also remove the total: text. 我在<p>标记内使用了<span> ,因为如果您尝试更新整个span标记,它也会删除total:文本。 I also trimmed up the jquery here a little bit by removing var x = $(this).closest(".item"); 我还通过删除var x = $(this).closest(".item");来对jquery进行了一些修整var x = $(this).closest(".item"); as we didn't need it in this case (not sure what that was in there for). 因为在这种情况下我们不需要它(不确定那里要做什么)。

$('#value span').text(y);

 $('.button').on('click', function() { var y = $(this).text(); $('#value span').text(y); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="list"> <div class="item"> <div>Item 1</div> <button class="button">50</button> </div> <div class="item"> <div>Item 2</div> <button class="button">30</button> </div> <div class="item"> <div>Item 3</div> <button class="button">70</button> </div> </div> <p id="value">Total: (<span>0</span>)</p> 

The Issue: 问题:

What you've asked jQuery to do is to append the text value of every object with a class of '.button', since all three buttons have that class, you're getting the text value of each button no matter which you click. 您要求jQuery执行的操作是为每个对象的文本值附加一个'.button'类,因为所有三个按钮都具有该类,因此无论您单击哪个按钮,您都将获得每个按钮的文本值。

The Solution: 解决方案:

Use the 'this' keyword! 使用“ this”关键字! In the context of a click listener, the 'this' keyword will refer to the control that fired the event. 在点击侦听器的上下文中,“ this”关键字将引用触发事件的控件。 In other words, the control that was clicked. 换句话说,被单击的控件。 Now you will only be appending the value of one button with each click. 现在,您每次点击只会附加一个按钮的值。

For Example: 例如:

 $(".list").on("click", ".button", function () { var y = $(this).text(); $('#value').append('(' + y + ')'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="list"> <div class="item"> <div>Item 1</div> <button class="button">50</button> </div> <div class="item"> <div>Item 2</div> <button class="button">30</button> </div> <div class="item"> <div>Item 3</div> <button class="button">70</button> </div> </div> <span id="value">Total:</span> 

Hope that helps! 希望有帮助!

** -EDIT- ** **-编辑-**

To REPLACE the value with each click you'd want to use the .text() method to insert the value into the text property of the control. 要在每次单击时替换该值,您想使用.text()方法将值插入到控件的text属性中。 Since you currently have "Total" in there, you'd also need to insert that programatically, or like I did, move it outside your span element. 由于您当前有“总计”,因此您还需要以编程方式插入该内容,或者像我一样将其移动到span元素之外。

 $(".list").on("click", ".button", function () { var y = $(this).text(); // Use .text instead of .append. $('#value').text('(' + y + ')'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="list"> <div class="item"> <div>Item 1</div> <button class="button">50</button> </div> <div class="item"> <div>Item 2</div> <button class="button">30</button> </div> <div class="item"> <div>Item 3</div> <button class="button">70</button> </div> </div> <div>Total: <span id="value"></span></div> 

Maybe just replace 也许只是替换

$('#value').append('(' + y + ')');

with

$('#value').html('(Total:' + y + ')');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM