我有一个div,里面有两个spans,我正在检查div中是否已经存在文本,如果存在,则更新span中的文本。
下面是我的div的结构:
<div class="top-cart-items">
<div class="top-cart-item clearfix">
<div class="top-cart-item-desc">Orange Club
<span class="top-cart-item-price">£2.99</span>
<span class="top-cart-item-quantity">x1</span>
</div>
</div>
<div class="top-cart-item clearfix">
<div class="top-cart-item-desc">Blue Tang
<span class="top-cart-item-price">£2.99</span>
<span class="top-cart-item-quantity">x1</span>
</div>
</div>
<div class="top-cart-item clearfix">
<div class="top-cart-item-desc">Soft Ball
<span class="top-cart-item-price">£3.99</span>
<span class="top-cart-item-quantity">x1</span>
</div>
</div>
</div>当在.top-cart-item-desc中找到字符串时,我希望使用jQuery更新.top-cart-item-quantity。有很多div和span具有相同的名称,所以我无法找到字符串的位置,也无法更新它。
这就是我到目前为止所尝试的。
$('.add').click(function(){
var name = $(this).attr("data-name");
var price = parseFloat($(this).attr("data-price"));
var quantity = 1;
if($('.top-cart-item-desc').text().indexOf(name) != -1)
{
$(this).closest('.top-cart-item-desc span.top-cart-item-quantity').text('15');
}
});我也尝试过siblings()和find(),但它们都不起作用。
这是一个fiddle
https://stackoverflow.com/questions/55786541
复制相似问题