在Ruby on Rails中的simple_form中添加字符计数器

时间:2016-01-16 14:07:39

标签: ruby-on-rails simple-form

我使用simple_form gem,我想在文本字段上使用简单的字符计数器。有人告诉我,这可能有用:

将其添加到表单中:

<%= f.input :body, id: "body-field" %>
<span id="body-count">0 characters</span>

和javascript:

$("#body-field").on("keyup", function(){
length = $(this).val().length;
$("#body-count").html(length);
});

我从这里得到了这些信息(注意:它充满了广告):http://www.sohua.xyz/questions-full/4320915/how-do-i-implement-a-basic-character-counter-in-a-simple-form

我这样做了,但没有任何反应。这是我的实际代码章节/ new.html.erb:

    <%= simple_form_for([@book, @book.chapters.build]) do |f| %>
    <%= f.input :chaptertitle %>
    Mininmum amount of characters: <%= @book.min_length %> Maximum amount of characters: <%= @book.max_length %>
    <%= f.input :chaptercontent, id: "body-field" %>
    <span id="body-count">0 characters</span>
    <%= f.input :author %>
    <%= f.button :submit %>
<% end %>

    <script>
      $("#body-field").on("keyup", function(){
        length = $(this).val().length;
        $("#body-count").html(length);
      });
    </script>

你能给我任何建议吗,如何让它发挥作用?

3 个答案:

答案 0 :(得分:3)

您可能想要使用js或coffee-script,我在下面提供了一个咖啡脚本示例:

将这段代码添加到 chapters.coffee 文件中:

ready = ->
  totalChars = 100
  #Total characters allowed
  countTextBox = $('#body-field')
  # Remaining chars count will be displayed here
  charsCountEl = $('#countchars')
  #initial value of countchars element
  charsCountEl.text totalChars
  #user releases a key on the keyboard
  countTextBox.keyup ->
    #get chars count in Text field
    thisChars = @value.replace(/{.*}/g, '').length
    if thisChars > totalChars
      # total extra chars to delete
      CharsToDel = thisChars - totalChars
      #remove excess chars from text field
      @value = @value.substring(0, @value.length - CharsToDel)
    else
      #count remaining chars
      charsCountEl.text totalChars - thisChars
    return
  return

$(document).ready ready
$(document).on 'page:load', ready
# Loads javascript while loading page

将此行添加到文本输入字段下方的表单中。

var ready;
var charsCountEl, countTextBox, totalChars;
totalChars = 100;
countTextBox = $('#body-field');
charsCountEl = $('#countchars');
charsCountEl.text(totalChars);
countTextBox.keyup(function() {
  var CharsToDel, thisChars;
  thisChars = this.value.replace(/{.*}/g, '').length;
  if (thisChars > totalChars) {
    CharsToDel = thisChars - totalChars;
    this.value = this.value.substring(0, this.value.length - CharsToDel);
  } else {
    charsCountEl.text(totalChars - thisChars);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="body-field" />
<span id="countchars"></span>

如果您的javascript /文件夹下的文件没有extestion .coffee,请将其重命名为chapters.coffee,如果确实如此,请将其重命名。

PS:这是同一http://pastebin.com/LZb1DAC4的javascript版本。

参考https://stackoverflow.com/a/24629105/2545197

答案 1 :(得分:2)

您需要将代码包装在jquery文档就绪函数中:

$(function() {
  $("#body-field").on("keyup", function(){
    var length = $(this).val().length;

    $("#body-count").html(length);
  });
});

为什么不使用现有的库? https://github.com/dtisgodsson/jquery-character-counter

答案 2 :(得分:0)

这个解决方案不如Abhinay的解决方案好,但它对我有用: (请注意,我是业余爱好者,这段代码可能很糟糕)

Javascript代码:

<script>

  $(document).ready(function(){

    var choosingbar = function( event ){
      $(event.target).parents(".counting").children(".count").text($(event.target).val().length);
    };

    $(".counting textarea").keyup(choosingbar);
  });

</script>

new.html.erb代码:

<%= simple_form_for([@book, @book.chapters.build]) do |f| %>
    <%= f.input :chaptertitle %>
    Mininmum amount of characters: <%= @book.min_length %> Maximum amount of characters: <%= @book.max_length %>
    <div class="counting" ><%= f.input :chaptercontent %>Characters: <span class="count"></span></div><br>
    <%= f.input :author %>
    <%= f.button :submit %>
<% end %>