带有输入字符倒计时的Rails简单表单

时间:2019-03-07 17:38:37

标签: javascript jquery ruby-on-rails

我正在尝试计算content input中的字符。倒计时工作正常,但是我唯一的问题是,当我加载页面时,它不计算在内容输入中添加的exisitng值。我想计算现有值,然后随着字符的添加或删除,我要相应地更新数字。如何实施这些更改?

  <%= simple_form_for @question, remote: true, :validate => true, html: { method: :post, id: :question_form } do |f| %>
  <%= f.error_notification %>
<span id="countchars"></span>. Maximum characters allowed 70.
  <%= f.input :content, label: false, :input_html => { :value => "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.", :id => "body-field" } %>
  <%= f.button :submit, "Ask", class:" btn btn-primary" %>
  <% end %>

<script>
  var ready;
  var charsCountEl, countTextBox, totalChars;
  totalChars = 70;
  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>

0 个答案:

没有答案