以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){
//提交代碼寫在這里
});
