jQuery実装ctrl+enter(リターン)提出フォーム

975 ワード

jQueryプラグインの開発方法で開発した.具体的なコードは以下の通りです.

jQuery.fn.extend({
  /**
   * ctrl+enter    
   * @param {Function} fn         
   * @param {Object} thisObj      
   */
  ctrlSubmit:function(fn,thisObj){
    var obj = thisObj || this;
    var stat = false;
    return this.each(function(){
      $(this).keyup(function(event){
        //   ctrl  ,  enter    
        if(event.keyCode == 17){
          stat = true;
          //    
          setTimeout(function(){
            stat = false;
          },300);
        } 
        if(event.keyCode == 13 && (stat || event.ctrlKey)){
          fn.call(obj,event);
        } 
      });
    });
  } 
});

使用方法:

$("#textarea").ctrlSubmit(function(event){
  //        
});

とても简単で実用的なのではないでしょうか.皆さんが好きになってほしいです.