0

jquery字符數不會顯示在我的評論表單下,即使它們適用於我的其他表單。任何幫助表示讚賞。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 

回答

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的。

+0

我將它轉換爲coffeescript,但它仍然不顯示。這是否與將這些行爲用作可評論的寶石有關? – Richard74

+0

最有可能,但我不熟悉那個插件。這可能有所幫助:http://juixe.com/techknow/index.php/2006/07/09/comments-on-acts-as-commentable/ – user2158494