如何使用jQuery检测在Web表单中单击了哪个提交按钮 [英] How to detect which submit button is clicked in a web form using jquery

查看:114
本文介绍了如何使用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
    });

这篇关于如何使用jQuery检测在Web表单中单击了哪个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆