2012-11-22 86 views
0
$(document).ready(function(){ 
    var keyUpTime = 0;     
    var t = 0; 
    var executeAfterOneSecond = false; 
    $('#source').keyup(function(){ 
     if(executeAfterOneSecond == false){ 
      executeAfterOneSecond = true; 
      t = setTimeout(function(){ 
       executeAfterOneSecond = false; 
       sendValue($('#source').val());       
       }, 600); 
      }         
     keyUpTime = $.now(); 
     setTimeout(function(){ 
      if($.now() - keyUpTime >= 200) { 
       clearTimeout(t); 
       executeAfterOneSecond = false; 
       sendValue($('#source').val());   
       }   
      },200); 
    }); 
}); 

<textarea id="source" name="text" wrap="soft" tabindex="0" dir="ltr" spellcheck="false" autocapitalize="off" autocomplete="off" autocorrect="off" style="box-sizing: border-box; overflow-y: hidden; overflow-x: auto; padding-right: 20px; " class="oggy-textarea"> 
           </textarea> 

嗯,我希望我的代码在change()上工作。 [$('#source')。change(function(){---});],不在keyup()上,但它没有。我尝试了bind(),但没有。怎么了?Textarea对.change()没有反应?

+0

你怎么知道这是行不通的?你做了什么实验,你期望得到什么结果,实际发生了什么? –

+0

我添加了一大堆编辑到我的答案 - 你可能会发现他们有帮助:) – Darren

回答

1

只有当input控件丢失focus时才会发生更改事件 - 因此它不会触发。

在这里看到下笔记:http://www.w3schools.com/jquery/event_change.asp

你必须去与keyUpkeyPresskeyDown

你真的可以清理你的方法了。类似以下内容可能会更稳定。

这将在最后一次关键帧后1秒后调用sendValue()。把你的22线低至6 :)

这里是下面的代码,作为一个例子,如果jsFiddle

$(document).ready(function(){ 

     $("#source").keyup(function(){ 
     clearTimeout(this.timer); 
     this.timer = setTimeout(function() {sendValue($("#source").val());}, 1000); 
     }); 

    });