字数统计

This commit is contained in:
yanxd 2013-12-28 13:16:22 +08:00
parent 1996283bd4
commit 69448b36ea
3 changed files with 39 additions and 34 deletions

View File

@ -1,4 +1,40 @@
<script>
var W3CDOM = document.createElement && document.getElementsByTagName;
window.onload = setMaxLength;
function setMaxLength() {
if (!W3CDOM) return;
var textareas = document.getElementsByTagName('textarea');
for (var i=0;i<textareas.length;i++) {
var textarea = textareas[i];
setMaxLengthItem(textareas[i]);
}
}
function setMaxLengthItem(textarea){
if (textarea.getAttribute('maxlength')) {
var counter = document.createElement('div');
counter.className = 'counter';
var counterClone = counter.cloneNode(true);
counterClone.innerHTML = '<span>0</span>/'+textarea.getAttribute('maxlength');
textarea.parentNode.insertBefore(counterClone,textarea.nextSibling);
textarea.relatedElement = counterClone.getElementsByTagName('span')[0];
textarea.onkeyup = textarea.onchange = checkMaxLength;
textarea.onkeyup();
}
}
function checkMaxLength() {
var maxLength = this.getAttribute('maxlength');
var currentLength = this.value.length;
if (currentLength > maxLength)
this.relatedElement.className = 'toomuch';
else
this.relatedElement.className = '';
this.relatedElement.firstChild.nodeValue = currentLength;
}
</script>
<% if jour.size >0 %>
<% remove_allowed = (User.current.id == jour.first.user_id) %>
<ul class="message-for-user">

View File

@ -1,36 +1,4 @@
<script>
var W3CDOM = document.createElement && document.getElementsByTagName;
window.onload = setMaxLength;
function setMaxLength() {
if (!W3CDOM) return;
var textareas = document.getElementsByTagName('textarea');
var counter = document.createElement('div');
counter.className = 'counter';
for (var i=0;i<textareas.length;i++) {
if (textareas[i].getAttribute('maxlength')) {
var counterClone = counter.cloneNode(true);
counterClone.innerHTML = '<span>0</span>/'+textareas[i].getAttribute('maxlength');
textareas[i].parentNode.insertBefore(counterClone,textareas[i].nextSibling);
textareas[i].relatedElement = counterClone.getElementsByTagName('span')[0];
textareas[i].onkeyup = textareas[i].onchange = checkMaxLength;
textareas[i].onkeyup();
}
}
}
function checkMaxLength() {
var maxLength = this.getAttribute('maxlength');
var currentLength = this.value.length;
if (currentLength > maxLength)
this.relatedElement.className = 'toomuch';
else
this.relatedElement.className = '';
this.relatedElement.firstChild.nodeValue = currentLength;
}
</script>
<%= form_tag({:controller => 'words', :action => 'create_reply'}, :remote => true) do %>
<%= text_area_tag 'user_notes', "", :class => 'noline',
:style => "resize: none;", :rows => 4,

View File

@ -5,9 +5,10 @@
) %>').hide();
$('#journal_reply_ul_<%=@jfm.m_parent_id%>').append(pre_append);
pre_append.fadeIn(600);
$('#project_respond_form_<%=@jfm.m_reply_id.to_s%> textarea').val('');
var textarea = $('#project_respond_form_<%=@jfm.m_reply_id.to_s%> textarea');
textarea.val('');
$('#project_respond_form_<%=@jfm.m_reply_id.to_s%>').hide();
setMaxLength();
setMaxLengthItem(pre_append.find('textarea')[0]);
<% else %>
alert("<%= l(:label_feedback_fail) %>");
<% end %>