【发布时间】:2019-01-17 02:25:24
【问题描述】:
我实现了一个表单 text_field 字符计数器,它可以在 jsfiddle 中工作,但不能在应用程序中使用。它出现但不会像在 jsfiddle 中那样实时计数。当我重新加载页面时,它将计算 text_field 中的当前字符。
我在页面中确实有更多的 javascript,但我尝试通过删除所有其他 java 而不是 coutner 来测试它,但它仍然无法工作。
奇怪的是我有一个
minlength: 20, maxlength: 1000
未验证时,将显示一个切换计数,并将实时计数,直到达到最小长度。我的目标是将该计数器翻译到 text_field 下,以便它始终显示/计算字符
这是我的代码:
<%= form.label :description %>
<%= form.text_area :description, class: "form-control message", :rows => 10, minlength: 20, maxlength: 1000, required: true %>
<span class="countdown"></span>
Jquery:
jQuery(document).ready(function ($) {
updateCountdownAll();
$('.message').live('input', updateCountdown);
});
function updateCountdownAll() {
$('.message').each(function () {
updateCountdown(this);
});
}
function updateCountdown(e) {
var currentElement;
if (e.target) {
currentElement = e.target;
} else {
currentElement = e;
}
var maxLengh = $(currentElement).attr('maxlength');
var remaining = $(currentElement).val().length;
$(currentElement).nextAll('.countdown:first').text(remaining + '/' + maxLengh);
}
JSFiddle: http://jsfiddle.net/4bozw3k5/
我可以对代码做些什么以使其在 rails 应用程序中工作?实时
【问题讨论】:
标签: javascript jquery ruby-on-rails ruby