我正在尝试计算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>