使用jquery获取表单操作/ URL

问题描述 投票:28回答:4

如何从带有jquery的表单中获取操作URL?

jquery forms asp.net-mvc-2
4个回答
67
投票

获取表单,询问action属性:

$('#myForm').attr('action');

18
投票

Need the full action url instead of just the basename?

通常,在抓住这些类型的值时,最好使用prop()而不是attr()。 (这个prop() vs attr() stackoverflow post解释了原因。)

但是,在这种情况下,@JAAulde answer正是我所需要的,但不是你需要的。您可能需要完整的操作网址。

考虑这个html表单开始标记:

<form action="handler.php" method="post" id="myForm">

attr() returns the exact action value:

$('#myForm').attr('action'); 
// returns 'handler.php'

prop() returns the full action url:

$('#myForm').prop('action');
// returns 'http://www.example.com/handler.php'

Check out the cool ascii table in this stackoverflow post了解更多。


3
投票

要使用表单的action属性,请使用:

$( '#myForm' ).attr( 'action' );

像@JAAulde说的那样。

要使用表单中输入的值,请使用:

$('#myInput').val();

2
投票

试试这个:

var formAction = $('#form')[0].action;

或者在表格提交上:

 $("#form").submit(function (event) {
    event.preventDefault();
    var frmAction=this.action;
});
© www.soinside.com 2019 - 2024. All rights reserved.