我使用的是 asp.net MVC,当我提交表单时,之前的开发人员嵌入了一些 jQuery 验证。
$('form').submit(function() {
...code done here to validate form fields
});
问题是表单上的“保存”和“取消”按钮都触发了这个提交 jQuery 函数。如果“取消”输入按钮被触发(id="cancel"name="cancel"value="cancel"),我不希望触发验证逻辑。 有没有一种方法可以在这个提交函数中检索按下哪个输入按钮以提交表单的 ID、名称或值?
最佳答案
我问了同样的问题:How can I get the button that caused the submit from the form submit event?
我能想出的唯一跨浏览器解决方案是:
$(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");
});
不确定它是否是您正在寻找的答案,但您应该将“取消”按钮更改为 anchor 标记。除非您正在处理表单值,否则无需提交取消。
关于javascript - jQuery 表单提交 : Any way to know what element triggered the submit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5640862/