Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript js.erb文件中返回的格式化编号_Javascript_Ruby On Rails_Erb - Fatal编程技术网

Javascript js.erb文件中返回的格式化编号

Javascript js.erb文件中返回的格式化编号,javascript,ruby-on-rails,erb,Javascript,Ruby On Rails,Erb,我需要将投票系统中返回的数字格式化为js.erb文件。当页面最初通过rails加载时,我可以为正数预先加一个“+”号(也可以为负数加一个“-”号),但在javascript上,我不确定如何添加相同的字符。这是我的_like.js.erb: $('.like') .on('ajax:send', function () { $(this).addClass('loading'); }) .on('ajax:complete', function () { $(this).remov

我需要将投票系统中返回的数字格式化为js.erb文件。当页面最初通过rails加载时,我可以为正数预先加一个“+”号(也可以为负数加一个“-”号),但在javascript上,我不确定如何添加相同的字符。这是我的_like.js.erb:

$('.like')
    .on('ajax:send', function () { $(this).addClass('loading'); })
    .on('ajax:complete', function () { $(this).removeClass('loading'); })
    .on('ajax:error', function () { $(this).after('<div class="error">There was an issue.</div>'); })
    .on('ajax:success', function (e, data, status, xhr) { $("#comment_<%= comment.id %>").html('<%=escape_javascript comment.cached_votes_score.to_s %>').hide().fadeIn(500); });

$('.unlike')
    .on('ajax:send', function () { $(this).addClass('loading'); })
    .on('ajax:complete', function () { $(this).removeClass('loading'); })
    .on('ajax:error', function () { $(this).after('<div class="error">There was an issue.</div>'); })
    .on('ajax:success', function (e, data, status, xhr) { $("#comment_<%= comment.id %>").html('<%=escape_javascript comment.cached_votes_score.to_s %>').hide().fadeIn(500); });
$('.like')
.on('ajax:send',function(){$(this).addClass('loading');})
.on('ajax:complete',function(){$(this).removeClass('load');})
.on('ajax:error',function(){$(this).after('出现问题');})
.on('ajax:success',function(e,data,status,xhr){$(“#comment#”).html(''..hide().fadeIn(500);});
$('.与'.''不同)
.on('ajax:send',function(){$(this).addClass('loading');})
.on('ajax:complete',function(){$(this).removeClass('load');})
.on('ajax:error',function(){$(this).after('出现问题');})
.on('ajax:success',function(e,data,status,xhr){$(“#comment#”).html(''..hide().fadeIn(500);});
这是我的html.erb部分:

<%= link_to unlike_post_post_comment_path(post, comment), class: "unlike", method: :put, remote: true do %>
    <button type="button" class="btn btn-xs btn-danger" aria-label="Left Align">
        <span><i class="fa fa-thumbs-o-down"></i></span>
    </button>
<% end %>
<%= link_to like_post_post_comment_path(post, comment), class: "like", method: :put, remote: true do %>
    <button type="button" class="btn btn-xs btn-info" aria-label="Left Align">
        <span><i class="fa fa-thumbs-o-up"></i></span>
    </button>
<% end %>
<% if comment.cached_votes_score > 0 %>
    <span class="badge like vote-score" id="comment_<%= comment.id %>"><%= "+ #{comment.cached_votes_score}" %></span>
<% elsif comment.cached_votes_score < 0 %>
    <span class="badge like vote-score" id="comment_<%= comment.id %>"><%= "- #{comment.cached_votes_score.abs}" %></span>
<% else %>
    <span class="badge like vote-score" id="comment_<%= comment.id %>"><%= comment.cached_votes_score %></span>
<% end %>

0 %>

好的,我知道你需要什么了。将
ajax:success
回调中的代码更改为如下内容:

$("#comment_<%= comment.id %>").html("<%= comment.cached_votes_score > 0 ? "+ #{comment.cached_votes_score.to_s}" : (comment.cached_votes_score < 0 ? "- #{comment.cached_votes_score.to_S}" : comment.cached_votes_score.to_s) %>");
$(“#comment#”)html(“0?”+#{comment.cached#u voces#u score.to#s}”:(comment.cached#u voces#u score<0?”-#{comment.cached#u voces#u score.to#s}:comment.cached#u voces;
看起来不漂亮,你可能需要稍微修改一下引号(改为“/”),但这是你想要的

编辑:

另一种方法是使用。不确定您是否特别希望在+和数字之间留一个空格,但请尝试以下操作:

$("#comment_<%= comment.id %>").html("<%= sprintf("%+d", comment.cached_votes_score) %>");
$(“#注释”)html(“”);

你能给我们看一下你有投票元素(
#comment
)的.html.erb文件吗?更新了原始帖子以反映。编辑了一点点(添加了转义的javascript)和一两句话,我们就可以开始了。谢谢酷!如果这样做有效(我猜您使用了第一种方法),不要忘记将其标记为已回答。