Ctrl+Enter提交表单

时间:13-04-11 栏目:Javascript, web前端 作者:zongyan86 评论:0 点击: 3,521 次

一,
<form onkeydown="if(event.ctrlKey&&event.keyCode==13)this.submit()">

二,

<div id="result"></div>  <textarea name="msg" id="msg" placeholder="输入内容" autofocus></textarea>   <button type="submit">提 交</button><span>可按“Ctrl+Enter”键提交</span>

textarea {display:block; width:450px;height:100px;border: 1px solid #ccc;}  button {border: 1px solid #ccc; background: #ececec;-webkit-border-radius: 3px;  -moz-border-radius: 3px;margin-top: 10px;padding: 5px 20px; cursor:pointer}  .post{width: 230px;border: 1px solid #ccc;background: #ececec; padding: 10px; margin: 10px 0;}

$.fn.ctrlEnter = function (btns, fn) {
var thiz = $(this);
btns = $(btns);

function performAction (e) {
fn.call(thiz, e);
};
thiz.bind("keydown", function (e) {
if (e.keyCode === 13 && e.ctrlKey) {
performAction(e);
e.preventDefault(); //阻止默认回车换行
}
});
btns.bind("click", performAction);
}

$("#msg").ctrlEnter("button", function () {
$("<p class='post'></p>").append(this.val().replace(/\n/g, "<br/>")).fadeIn('slow')
.appendTo("#result");
this.val("");
});



声明: 本文由( zongyan86 )原创编译,转载请保留链接: Ctrl+Enter提交表单

关注我们