使用 jquery 获取表单操作/url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5620420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a form action/url with jquery
提问by Dejan.S
How do I get the action url from a form with jquery?
如何从带有 jquery 的表单中获取操作 url?
回答by JAAulde
Get the form, ask for the action
attribute:
获取表单,请求action
属性:
$('#myForm').attr('action');
回答by elbowlobstercowstand
Need the fullaction url instead of just the basename?
需要完整的操作 url 而不仅仅是基本名称?
Generally it's better to use prop()
instead of attr()
when grabbing these sorts of values. (This prop() vs attr() stackoverflow postexplains why.)
通常最好使用prop()
而不是attr()
获取这些类型的值。(这个prop() vs attr() stackoverflow 帖子解释了原因。)
However, in this instance, @JAAulde answeris exactly what Ineeded, but not might be what youneed. You might want the full action url.
但是,在这种情况下,@JAAulde 答案正是我所需要的,但可能不是您所需要的。您可能需要完整的操作网址。
Consider this html form start tag:
考虑这个 html 表单开始标签:
<form action="handler.php" method="post" id="myForm">
attr() returns the exact action value:
attr() 返回确切的操作值:
$('#myForm').attr('action');
// returns 'handler.php'
prop() returns the full action url:
prop() 返回完整的动作 url:
$('#myForm').prop('action');
// returns 'http://www.example.com/handler.php'
Check out the cool ascii table in this stackoverflow postto learn more.
查看此 stackoverflow 帖子中的酷 ascii 表以了解更多信息。
回答by Owen
To use the action attribute of a form use:
要使用表单的 action 属性,请使用:
$( '#myForm' ).attr( 'action' );
like @JAAulde said.
就像@JAAulde 说的那样。
To use an entered value from a form use:
要使用表单中输入的值,请使用:
$('#myInput').val();
回答by Fereydoon Barikzehy
Try this one:
试试这个:
var formAction = $('#form')[0].action;
Or on form sumit:
或者在表格上:
$("#form").submit(function (event) {
event.preventDefault();
var frmAction=this.action;
});