Home >Web Front-end >JS Tutorial >jQuery implements ctrl enter (carriage return) to submit the form_jquery

jQuery implements ctrl enter (carriage return) to submit the form_jquery

WBOY
WBOYOriginal
2016-05-16 15:36:161218browse

Developed using jQuery plug-in development method. The specific code is as follows:

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);
        } 
      });
    });
  } 
});

How to use:

$("#textarea").ctrlSubmit(function(event){
  //提交代码写在这里
});

Isn’t it very simple and practical? I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn