2017-03-17 19 views
0

它遲到了很可能是我完全缺失和愚蠢的東西,但爲什麼它不更新單擊元素上的數據值和跨度內的計數的任何想法?jQuery點擊函數來獲取數據計數並應用於跨度計數

// simple like count function for demo 
 
$('.action-like').on('click tap', function() { 
 
    var currentCount = $(this).data('like-count'); 
 
    var newCount = currentCount + 1; 
 
    $(this).data('like-count', newCount); 
 
    $(this).find('.count').html(newCount); 
 

 
    return false; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<p><a href="#" class="btn btn--secondary action-like" data-like-count="1">Like</a> <span class="small"><span class="count">1</span> person likes this</span> 
 
</p> 
 

 
<p><a href="#" class="btn btn--primary action-like" data-like-count="5">Like</a> <span class="small">You and <span class="count">5</span> other people like this</span> 
 
</p>

+0

不要忘記標記答案及/給予好評,如果其中任何一個就足夠了。 –

+0

我知道,我之前沒有讓我知道,因爲在我可以標記它之前必須有一定的/最少的分鐘數。 – James

回答

1

這是因爲在a鏈接內沒有.count。你可以通過遍歷父母來解決它,然後做你的find

像這樣:

$('.action-like').on('click tap', function() { 
    var currentCount = $(this).data('like-count'); 
    var newCount = currentCount + 1; 
    $(this).data('like-count', newCount); 
    $(this).parent().find('.count').html(newCount); 
    return false; 
}); 
+0

是的,我剛剛注意到,哈哈感謝隊友 – James

0

試試這個

$(this).parent().find('.count').html(newCount); 

的關鍵是,你this被引用到這是sibiling的.counta