Jquery字符数没有显示

时间:2013-04-03 15:41:34

标签: javascript jquery ruby-on-rails coffeescript acts-as-commentable

Jquery字符计数未显示在我的评论表单下,即使它们适用于我的其他表单。任何帮助表示赞赏。

_form.html.erb

<%= form_for [@commentable, @comment] do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <div class="field">
    <%= f.text_field :title %>
    <div class="titlecountdown"></div>
    <%= f.text_area :content %>
    <div class="contentcountdown"></div>
  </div>
  <div class="actions">
    <%= f.submit "Post", class: "btn btn-large btn-primary" %>
  </div>
<% end %>

custom.js.coffee

$ ->
  updateTitleCountdown = ->
    remaining = 20 - jQuery("#comment_title").val().length
    jQuery(".titlecountdown").text remaining + " characters remaining"

  jQuery ->
    updateTitleCountdown()
    $("#comment_title").change updateTitleCountdown
    $("#comment_title").keyup updateTitleCountdown

$ ->
  updateContentCountdown = ->
    remaining = 120 - jQuery("#comment_content").val().length
    jQuery(".contentcountdown").text remaining + " characters remaining"

  jQuery ->
    updateContentCountdown()
    $("#comment_content").change updateContentCountdown
    $("#comment_content").keyup updateContentCountdown

1 个答案:

答案 0 :(得分:0)

jQuery(document).ready(function($) {
    updateCountdown();
    $('.question').change(updateCountdown);
    $('.question').keyup(updateCountdown);
});

function updateCountdown() {
    var remaining = 100 - jQuery('.question').val().length;
    jQuery('.countdown').text(remaining + ' characters remaining.');
}

这对我来说非常有用,尽管不是在coffeescript中。