javascript - 如何用字形替换数据切换文本? (使用 Rails 和 JS)

标签 javascript jquery ruby-on-rails ruby

我正在使用acts_as_votable gem。现在,“喜欢”和“不喜欢”按钮可以正常工作,并且“喜欢”已保存到数据库中。我需要的是将“喜欢”和“不喜欢”文本替换为字形。它现在不起作用,因为 data: {toggle_text: 'Like' 部分仅接受文本值,并且 JS 连接到 data-toggle-text。

我应该如何处理这个问题?感谢您的帮助。

likes.js.coffee 文件

$(document).on 'ajax:success', 'a.vote', (status,data,xhr)->
  # update counter
  $(".votes-count[data-id=#{data.id}]").text data.count

  # toggle links
  $("a.vote[data-id=#{data.id}]").each ->
    $a = $(this)
    href = $a.attr 'href'
    text = $a.text()
    $a.text($a.data('toggle-text')).attr 'href', $a.data('toggle-href')
    $a.data('toggle-text', text).data 'toggle-href', href
    return

  return

show.html.erb 文件

<% if current_user.liked? @review %>
<%= link_to "Dislike", dislike_review_path(@review), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Like', toggle_href: like_review_path(@review), id: @review.id } %>
<% else %>
<%= link_to "Like", like_review_path(@review), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Dislike', toggle_href: dislike_review_path(@review), id: @review.id } %>
<% end %>

<span class="votes-count" data-id="<%= @review.id %>">
<%= @review.get_likes.size %>
</span>
users like this
<br>

最佳答案

你可以尝试做类似的事情

喜欢.js.coffee

$(document).on 'ajax:success', 'a.vote', (status,data,xhr)->
  # update counter
  $(".votes-count[data-id=#{data.id}]").text data.count

  # toggle links
  $("a.vote[data-id=#{data.id}]").each ->
    $a = $(this)
    href = $a.attr 'href'
    text = $a.html()
    $a.html($a.data('toggle-text')).attr 'href', $a.data('toggle-href')
    $a.data('toggle-text', text).data 'toggle-href', href
    return

  return

show.html.erb

<% if current_user.liked? @review %>
  <%= link_to '<i class="glyphicon glyphicon-star"></i>'.html_safe, dislike_review_path(@review), class: 'vote', method: :put, remote: true, data: { toggle_text: "<i class='glyphicon glyphicon-star-empty'></i>".html_safe, toggle_href: like_review_path(@review), id: @review.id } %>
<% else %>
  <%= link_to '<i class="glyphicon glyphicon-star-empty"></i>'.html_safe, like_review_path(@review), class: 'vote', method: :put, remote: true, data: { toggle_text: "<i class='glyphicon glyphicon-star'></i>".html_safe, toggle_href: dislike_review_path(@review), id: @review.id } %>
<% end %>

<span class="votes-count" data-id="<%= @review.id %>">
<%= @review.get_likes.size %>
</span>
users like this
<br>

关于javascript - 如何用字形替换数据切换文本? (使用 Rails 和 JS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42469374/

相关文章:

javascript - 我可以使用 javascript 更改地址栏中的 URL 字符串吗

javascript - Fido U2F 客户端 JavaScript 源代码

javascript - clearInterval 不重置

javascript - 如何在 Jquery 数据表中根据条件隐藏列?

ruby-on-rails - Rails 模型到递归 json

ruby-on-rails - 如何从 Rails 中的同一页面提交多个重复的表单 - 最好使用一个按钮

ruby-on-rails - 更新 Heroku 生产数据库中的字符串属性

javascript - 动态调用extjs类中的函数

javascript - Bootstrap : 'TypeError undefined is not a function' /'has no method ' tab'' when using bootstrap-tabs

jquery - 如何将 CKEDITOR.dom.element 的实例转换为 CKEDITOR 中的 jquery 对象?