jQuery表单提交:任何方式知道什么元素触发提交? [英] jQuery form submit: Any way to know what element triggered the submit?

查看:174
本文介绍了jQuery表单提交:任何方式知道什么元素触发提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net MVC,当我提交表单时,以前的开发人员已经嵌入了一些jQuery验证。

I'm using asp.net MVC and when I submit a form, a previous developer had embedded some jQuery validation.

$('form').submit(function() {
    ...code done here to validate form fields
});

问题在于表单上的保存和取消按钮会触发此提交jQuery功能。如果取消输入按钮被触发(id =cancelname =cancelvalue =cancel),我不希望验证逻辑触发。
在这个提交函数中,有没有一种方法可以检索按下哪个输入按钮来提交表单的ID,名称或值?

The problem is that both the "Save" and "Cancel" buttons on the form fire this submit jQuery function. I don't want the validation logic to fire if the "Cancel" input button was fired (id="cancel" name="cancel" value="cancel"). Is there a way that, within this submit function, I can retrieve the ID, name or value of which input button was pressed to submit the form?

推荐答案

我问了同样的问题:如何获取导致从表单提交事件提交的按钮?

只有跨浏览器的解决方案,我可以拿出这个:

The only cross-browser solution I could come up with was this:

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

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

    // DO WORK

});

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






不确定如果它是你的答案寻找,但你应该改变取消按钮到一个锚标记。除非您正在处理表单值,否则无需提交取消。


Not sure if its the answer you're looking for but you should change the "Cancel" button to an anchor tag. There's no need to submit a cancel unless you're doing work on the form values.

这篇关于jQuery表单提交:任何方式知道什么元素触发提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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