jquery获取帖子动作网址

djb*_*ick 73 jquery

我正在尝试在jquery函数中访问post目标操作.

例:

<form action="/page/users" id="signup" method="post">
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我想访问"action"部分 - "/ page/users".

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

好像我错过了很简单的东西.我在dom中看到了值,但不知道它在jquery中的存储位置.

谢谢!

Amy*_*y B 115

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

    alert('the action is: ' + $form.attr('action'));
});
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考:`live()`在1.7中弃用,在1.9中删除.Devs现在应该使用`on()`代替. (12认同)
  • 不是第二行不必要,第三行就不像警报('动作是:'+ $(this).attr('action')); (4认同)

Vik*_*wal 15

试试这个ocde ;;

var formAction = $("#signup").attr('action');
Run Code Online (Sandbox Code Playgroud)


Fer*_*ehy 10

干净简单:

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

      alert(this.action);
});
Run Code Online (Sandbox Code Playgroud)