0

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
4

1 回答 1

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.');
}

这对我来说非常有效,虽然不是在咖啡脚本中。

于 2013-04-03T15:52:46.910 回答