我如何从表单提交事件中获取导致提交的按钮? [英] How can I get the button that caused the submit from the form submit event?

查看:178
本文介绍了我如何从表单提交事件中获取导致提交的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找触发表单提交的提交"按钮的值

I'm trying to find the value of the submit button that triggered the form to submit

$("form").submit(function() {

});

我可能会为每个按钮触发$("input [type = submit]").click()事件并设置一些变量,但这似乎不如将按钮从提交表单时拉出表单的方式优雅

I could possibly fire a $("input[type=submit]").click() event for each button and set some variable, but that seems less elegant than some how pulling the button off of the the form on submit.

推荐答案

我实现了这个功能,我想它会做到的.

I implemented this and I suppose it will do.

$(document).ready(function() {
    $("form").submit(function() { 

    var val = $("input[type=submit][clicked=true]").val()

    // DO WORK

});

这是设置它的提交按钮事件

and this is the submit button event that sets it up

$("form input[type=submit]").click(function() {
    $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
    $(this).attr("clicked", "true");
});

感谢您的回复,但这并不是非常优雅……

Thanks for the responses, but this isn't terribly inelegant...

这篇关于我如何从表单提交事件中获取导致提交的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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