jQuery获取后操作URL


80

我正在尝试在jquery函数中访问post target操作。

例:

<form action="/page/users" id="signup" method="post">

在这种情况下,我想访问“操作”部分-“ / page / users”。

$('#signup').live("submit", function(event) {
    // get this submitted action
}

似乎我缺少一些非常简单的东西。我在dom中看到了值,但不知道它在jquery中的存储位置。

谢谢!

Answers:


125
$('#signup').on("submit", function(event) {
    $form = $(this); //wrap this in jQuery

    alert('the action is: ' + $form.attr('action'));
});

4
第二行不是不必要的,第三行是否可以像alert('action is:'+ $(this).attr('action'));
Mohd Abdul Mujib 2014年

1
正确的@ 9kSoft,但我们有个主意:)
Kennyomar 2015年

12
仅供参考:live()在1.7中已弃用,在1.9中已删除。开发人员现在应该on()改用。
vegemite4me,2015年

当表单具有多个具有不同操作的提交按钮时,这似乎不起作用。
keoghpe

抱歉,但是如果您要使用formaction属性w3schools.com/tags/att_button_formaction.asp
zdarsky.peter,


11

简洁:

$('#signup').submit(function(event) {

      alert(this.action);
});

不要依赖此属性,因为它可能返回具有name的子节点的列表action

首先,action属性的唯一用途是在htmlform标记内。其次,嵌套forms并不是最佳实践,也不是进行完全优化的好主意Html DOM
Fereydoon Barikzehy
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.