如何使用jQuery检测在Web表单中单击了哪个提交按钮
问题描述:
如果我在表单上有2个提交按钮:
If I have 2 submit buttons on a form:
<input name="submit" type="submit" value="Button 1">
<input name="submit" type="submit" value="Button 2">
我有这个jquery,但每次单击按钮2时,都会收到按钮1的警报.
I have this jquery but every time I click button 2 I get the alert for button 1.
$('form.profile').submit(function() {
if ($("input[type=submit]").val() == 'Button 1')
{
alert('you clicked button 1');
return false;
}
elseif ($("input[type=submit]").val() == 'Button 2')
{
alert('you clicked button 2');
return false;
}
});
在将表单发送到服务器之前,我需要执行表单验证,并且需要能够确定单击了两个按钮中的哪个按钮.
I need to perform form validation before the form is sent to the server and I need to be able to determine which of the 2 buttons was clicked.
是否可以识别2个按钮中的哪个按钮导致使用jquery提交表单?
Is it possible to identify which of the 2 buttons caused the form to be submitted using jquery?
答
此帮助!!! http://jsfiddle.net/hun4U/
您可以使用click事件来代替formSubmission.
Instead of formSubmission you can use click events.
HTML:-
<input name="submit" id="submit1" type="submit" value="Button 1">
<input name="submit" id="submit2" type="submit" value="Button 2">
脚本
$('#submit1').click(function(){
//do your actions here
});
$('#submit2').click(function(){
//do your actions here
});