我有以下内容:
<span class="label-info">3</span>
我有以下jquery
var replaceit = $(this).closest(':has(.label-info)').find('.label-info').text();
变量的值总是一个整数,但不会总是 3:
ie: 1, 2, 3, 4, 5.
我已经尝试了很多方法,但无法获得更改的值。我最近的尝试是:
return $(this).closest(':has(.label-info)').html().replace(replaceit, (replaceit - 1));
我的最终结果是,无论“lable-info”的当前值是多少,都减去 1,然后用这个新结果切换它。因此,基于值 3 的新跨度将变为。
<span class="label-info">2</span>
我如何实现这一目标?
更新代码更清晰
html:
<div>
<span class="lable-info">3</span>
</div>
<div>
<a class="accept_friend">accept</a>
</div>
JavaScript:
$(document).on("click", "a.accept_friend", function() {
var checkValue = $(this).closest(':has(.name)').find('.name').text();
var removeit = $(this).closest(':has(.item)').find('.item').fadeOut();
var replaceit = $(this).closest(':has(.label-info)').find('.label-info').text();
$.ajax({
url: '/includes/accept_friend.php',
type: 'post',
data: {checkValue},
success:function(data){
return removeit;
$("a.remove_pending").text(function () {
return ('.label-info').html().replace(replacei, replaceit);
});
}
注意事项:
我没有使用 id。我正在使用类。有多个具有相同名称的类。所以我必须调用最近的。
最佳答案
从跨度中获取一个值,将其减去 1 并更新跨度(使用 jQuery):
HTML
<span class="label-info">3</span>
jQuery
var n = $(".label-info").text(),
n2 = n - 1;
$(".label-info").text(n2);
希望能帮到你
关于javascript - 使用 jQuery 更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39463021/