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
Release: 2016-05-16 15:36:16
Original
1209 people have browsed it

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);
        } 
      });
    });
  } 
});
Copy after login

How to use:

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template