![](/img/trans.png)
[英]Like button increment counter not working. PHP, AJAX, MySQL, javascript
[英]Counter is not working - implementing a like button inside php
我試圖在點擊后實現喜歡的按鈕,它應該增加點贊的次數,但是我試圖在php代碼內部實現,但是它無法正常工作,我無法弄清這里的問題是什么?
<html> <script> $(".like_button button").on("click", function() { var $count = $(this).parent().find('.count'); $count.html($count.html() * 1 + 1); }); </script> <?php echo '<div class="like_button"> <button>Like</button> <span class="count">0</span> </div>'; ?> </html>
為什么此代碼不起作用?
這是因為您要在按鈕存在之前就對其進行選擇。 將$(".like_button button")
之后 :
<?php
echo '<div class="like_button">
<button>Like</button>
<span class="count">0</span>
</div>';
?>
<script>
$(".like_button button").on("click", function() {
var $count = $(this).parent().find('.count');
$count.html($count.html() * 1 + 1);
});
</script>
編輯:
跟進您的注釋,您尚未包括jQuery,但您嘗試使用它( $
)。 那么解決方案很簡單:包括jQuery。 並打開您的控制台 。
工作片段:
$(".like_button button").on("click", function() { var $count = $(this).parent().find('.count'); $count.html($count.html() * 1 + 1); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="like_button"> <button>Like</button> <span class="count">0</span> </div> <div class="like_button"> <button>Like</button> <span class="count">0</span> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.