简体   繁体   English

获取表单的提交URL作为相对URL?

[英]Getting a form's submit URL as a relative url?

Let's say I have a form located at: 假设我有一个表格位于:

http://www.mysite.com/some/place/some/where/index.php

And I have this form: 我有这种形式:

<form action="submit.php"> ... </form>

If I do this: 如果我这样做:

$("form").attr("action");

I will get: 我会得到:

submit.php

What is a foolproof way to get the action from the form, so that it includes the entire URL: 从表单中获取操作的一种万无一失的方法是使它包含整个URL:

http://www.mysite.com/some/place/some/where/submit.php

?

Try 尝试

$("form").prop("action");

or (assuming your form is in the same folder as the page and the page URL ends with /something) 或(假设您的表单与页面位于同一文件夹中,并且页面URL以/ something结尾)

var loc = location.href;
var page = loc.substring(0,loc.lastIndexOf("/")+1)+
  $("form").attr("action");

Handle query strings and hashes especially for Quentin: 处理查询字符串和哈希值,尤其是对于Quentin:

Live Demo 现场演示

var loc = location.origin + location.pathname;
var action = $("form").attr("action").split("/").pop();
var page = loc.substring(0,loc.lastIndexOf("/"))+"/"+action;
var el = document.createElement('a');
el.href = $("form").prop("action");

alert(el.href);

Chrome adjusts the href to give the absolute url, not sure with other browsers Chrome会调整href以提供绝对网址,其他浏览器不确定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM