抓取表单的提交按钮

时间:2017-05-22 17:56:45

标签: javascript jquery

我正在尝试抓取表单的提交按钮,以便从提交按钮中删除disabled属性。

我在听表格提交:

$(".some-class-on-the-form").submit(function(e) {
  ...
});

我可以在该侦听器的上下文中找到该表单:

this.closest('form');

现在我需要抓住与此表单关联的submit按钮。我不想在此提交按钮中添加idclass,因为我需要将其用于一堆表单。

我能想到的最简单方法是使用type="submit"搜索元素,但它会一直出错:

this.closest('form').find("input[type=submit]");

在控制台中返回此错误:

  

TypeError:this.closest('form')。find不是函数。 (在'this.closest('form')。find(“input [type = submit]”)','this.closest('form')。find'is undefined)

我也尝试了这个建议here,但我得到了同样的错误:

this.closest('form').find(':submit');

我也审核了jquery doc on :submit

问题:我觉得表单很好。现在:如何找到与表单关联的提交按钮?

2 个答案:

答案 0 :(得分:1)

$(this).closest('form').find('button[type=submit]')

这样就可以了。

答案 1 :(得分:0)

尝试将表单包装为jQuery对象:

$(this.closest('form')).find("input[type=submit]");
相关问题