javascript - 如何从js中的按钮单独提取评分和?
问题描述
由于不了解 js,当单击按钮(其中一种反应)时,我无法使其工作和更新,只有一个计数器,而不是内置在按钮中。据我了解,错误是函数中的 $ (this) 。但是有什么方法可以改变按钮之间的评分量计数器呢?除此之外,此代码还添加到另外 9 个帖子中。
<button class="btn-like-up" data-id="{{ post.id }}" type="button" data-type="file" data-action="rate_up">
<i class="bi bi-chevron-up"></i>
</button>
<!-- COUNTER -->
<span data-count="total_rating">{{ post.total_rating }}</span>
<!-- / COUNTER -->
<button class="btn-like-down" data-id="{{ post.id }}" type="button" data-type="file" data-action="rate_down">
<i class="bi bi-chevron-down"></i>
<span data-count="total_rating">{{ post.total_rating }}</span>
</button>
单独使用时,点击时不会更新评分数字。
$('.btn-like.up').submit('click', function(e) {
e.preventDefault()
let ratingBox = $(this).data('id')
let postType = ratingBox.data('type')
let postId = ratingBox.data('id')
let postAction = ratingBox.data('action')
let totalRating = ratingBox.find('[data-count="total_rating"]')
$.ajax({
type: 'POST',
url: '/system/' + postType + '/' + postId + '/' + postAction + '/',
data: {
'post_id': postId,
'csrfmiddlewaretoken': csrftoken
},
success: function(response) {
totalRating.text(response.total_rating);
},
error: function(error) {
console.log(error);
},
})
})
$('.btn-like.down').submit('click', function(e) {
e.preventDefault()
let ratingBox = $(this).data('id')
let postType = ratingBox.data('type')
let postId = ratingBox.data('id')
let postAction = ratingBox.data('action')
let totalRating = ratingBox.find('[data-count="total_rating"]')
$.ajax({
type: 'POST',
url: '/system/' + postType + '/' + postId + '/' + postAction + '/',
data: {
'post_id': postId,
'csrfmiddlewaretoken': csrftoken
},
success: function(response) {
console.log(response)
totalRating.text(response.total_rating);
},
error: function(error) {
console.log(error);
},
})
})
解决方案
您可以给data-span="{{ post.id }}"
您的totalRating
跨度。然后,每当用户单击任何按钮时,您都可以获得data-id
并使用它来引用跨度标签,即:$("span[data-span=" + postId + "]")
。此外,您可以将两个按钮事件合并为一个,因为我看不出它们之间有任何区别。
演示代码:
//use only one ..event handler ..
$('.btn-like-up ,.btn-like-down').on('click', function(e) {
e.preventDefault()
let ratingBox = $(this) //refer to your button
let postType = ratingBox.data('type')
let postId = ratingBox.data('id')
let postAction = ratingBox.data('action')
let totalRating = $("span[data-span=" + postId + "]") //refer to span..tag between button
console.log(postId, postAction)
/*$.ajax({
type: 'POST',
//other codes
success: function(response) {*/
//just for demo..
var response = {
"total_rating": 90
}
totalRating.text(response.total_rating);
/* },
error: function(error) {
console.log(error);
},
})*/
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<button class="btn-like-up" data-id="1" type="button" data-type="file" data-action="rate_up">
<i class="bi bi-chevron-up">Up</i>
</button>
<!--added data-span="{{ post.id }}"-->
<span data-span="1" data-count="total_rating">12</span>
<button class="btn-like-down" data-id="1" type="button" data-type="file" data-action="rate_down"><i class="bi bi-chevron-down">Down</i>
</button>
<br/>
<button class="btn-like-up" data-id="2" type="button" data-type="file" data-action="rate_up">
<i class="bi bi-chevron-up">Up</i>
</button>
<span data-span="2" data-count="total_rating">12</span>
<button class="btn-like-down" data-id="2" type="button" data-type="file" data-action="rate_down"><i class="bi bi-chevron-down">Down</i>
</button>
推荐阅读
- php - Composer 在调用 autoload.php 后执行脚本
- ads - 如何预测广告展示次数以避免超卖?
- bash - 如何在没有sed的情况下替换csv文件中的一行
- java - Prometheus:如何收集临时进程的内存消耗指标?
- javascript - v-for 没有正确对齐内容?
- windows - 在 .idea/* 文件中强制使用 LF 行分隔符
- javascript - 带有文本框的 MvcGrid 复选框未显示正确的文本框值
- ios - 适用于 Android 和 iOS 平台的 Ionic 3 应用程序上的 PDF 到 Base64
- machine-learning - 从以前的交易中预测客户行为
- angular - 将 API 与 Angular Async 一起使用的最佳方式