如何在输入按键和两个提交按钮时区分表单提交 [英] how to differentiate form submition on enter keypress and two submit buttons
问题描述
我有一个带有两个提交按钮的表单(具有不同ID的提交按钮).我想对:
I have one form with two submit buttons (submit buttons with different IDs). I want to take different actions on :
- 输入按键
- 点击提交按钮#1
- 点击提交按钮#2
有人可以指导我,如何通过jQuery实现这一目标.
Can some one guide me, how can I achieve this through jQuery.
预先感谢
推荐答案
您可以执行以下操作(请参阅实时示例):
You could do the following (See Live Example):
$(document).ready(function(){
$("input[type='text'],input[type='password']").keypress(function(e){
e.preventDefault();
if (e.which == 13){
alert("User pressed 'Enter' in a text input.");
// do your custom processing here
}
});
$("input[type='submit']").click(function(e){
e.preventDefault();
var submitClicked = e.target.id;
alert("The submit button with id '"+submitClicked+"' was clicked!");
//probably a switch statement here to do your custom processing based on which submit button was pressed.
});
});
上面的代码将陷阱在任何文本或密码输入上输入Enter键...将忽略<textarea>
上的Enter键输入(我认为这是理想的效果).在任何文本或密码输入上按Enter键都不会触发第一次提交按钮的点击.
The code above will trap for enter keypress on any text or password input... will ignore enter key presses on <textarea>
(a desired effect I would think). Pressing enter on any text or password input will NOT trigger the first submit button click.
请注意,您必须使用$([your form selector]).submit();
这篇关于如何在输入按键和两个提交按钮时区分表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!