简体   繁体   English

观察者在IE中不起作用

[英]observer does not work in IE

In my ruby on rails app I am trying to use a Prototype Form.Element.Observer to run a count of the characters in a message. 在我的ruby on rails应用程序中,我尝试使用Prototype Form.Element.Observer来运行消息中的字符计数。 This works fine on Firefox/Safari/Chrome, but not on IE. 这适用于Firefox / Safari / Chrome,但不适用于IE。 On IE the observer simply does not fire. 在IE上,观察者根本不会开火。 Is there a fix or a different way of doing this? 有没有修复或不同的方式这样做?

My ruby tag looks like this: 我的ruby标签看起来像这样:

<%= countdown_field('txtmsg[memo]','memo-counter', 141, :frequency => 0.10) %>

The countdown_field function looks like this: countdown_field函数如下所示:

  def countdown_field(field_id,update_id,max,options = {})
    function = "$('#{update_id}').innerHTML = (#{max} - $F('#{field_id}').length);"
    count_field_tag(field_id,function,options)
  end

  def count_field_tag(field_id,function,options = {})  
    out = javascript_tag function, :defer => 'defer'
    out += observe_field(field_id, options.merge(:function => function))
    return out
  end

The resultant HTML looks like this: 生成的HTML如下所示:

<textarea class="memo-tag text txtmsg-memo" id="txtmsg[memo]" name="txtmsg[memo]" />
<p>You have <span id="memo-counter">...</span> characters left.</p>

<script defer="defer" type="text/javascript">
  $('memo-counter').innerHTML = (141 - $F('txtmsg[memo]').length);
</script>
<script type="text/javascript">
  new Form.Element.Observer('txtmsg[memo]', 0.1, function(element, value) {
                  $('memo-counter').innerHTML = (141 - $F('txtmsg[memo]').length);})
</script>

First of all you need to add a closing tag for your <textarea> element because it can't be self-closing, and the cols and rows attributes are mandatory. 首先,您需要为<textarea>元素添加结束标记,因为它不能自动关闭,并且colsrows属性是必需的。

Using the code below I can partially get it working for IE. 使用下面的代码我可以部分地使它适用于IE。 It decrements the counter as you type characters, but for some reason the Delete, Backspace and cursor keys don't work when using IE6! 它在您键入字符时递减计数器,但由于某种原因,使用IE6时,Delete,Backspace和光标键不起作用! It works fine using Firefox 3.6. 它使用Firefox 3.6工作正常。

<textarea class="memo-tag text txtmsg-memo" id="txtmsg[memo]" cols="40" rows="2" name="txtmsg[memo]"></textarea>
<p>You have <span id="memo-counter">...</span> characters left.</p>
<script type="text/javascript">
  new Form.Element.Observer("txtmsg[memo]", 0.1, function(element, value) {
    $("memo-counter").update(141 - value.length);
  });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM