jQuery 使用“a href”构建动态 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20834002/
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
Building dynamic URL using 'a href'
提问by abhishek ameta
I am using spring-3.2 version.
我正在使用 spring-3.2 版本。
@RequestMapping("/company={companyId}/branch={branchId}/employee={employeeId}/info")
The requestmapping is used to map a URL, so in this case when ever a URL is called using
requestmapping 用于映射 URL,因此在这种情况下,当使用 URL 调用 URL 时
<a href="company=1/branch=1/employee=1/info" > employee info </a>
the method is called in the controller with the exact @RequestMapping annotation, now I want to create the "a href" tag dynamically and want to create companyId,branchId,or employeeId dynamically.
在控制器中使用精确的@RequestMapping 注释调用该方法,现在我想动态创建“a href”标签并想动态创建 companyId、branchId 或 employeeId。
回答by Pantelis Natsiavas
You could of course build the string pointing to the respective URL dynamically.
您当然可以动态构建指向相应 URL 的字符串。
A first option would be using a javascript function. However, even this function has to take the IDs from somewhere. In my example, I suppose that there are javascript variables which already contain the right IDs.
第一个选项是使用 javascript 函数。然而,即使是这个函数也必须从某个地方获取 ID。在我的示例中,我假设存在已经包含正确 ID 的 javascript 变量。
function createDynamicURL()
{
//The variable to be returned
var URL;
//The variables containing the respective IDs
var companyID=...
var branchID=...
var employeeID=...
//Forming the variable to return
URL+="company=";
URL+=companyID;
URL+="/branch=";
URL+=branchID;
URL+="/employee=";
URL+=employeeID;
URL+="/info";
return URL;
}
Then your html would be like:
那么你的 html 会是这样的:
<a href="javascript:window.location=createDynamicURL();" > employee info </a>
Another, more elegant solution would be to use the onClick event:
另一个更优雅的解决方案是使用 onClick 事件:
<a href="#" onclick="RedirectURL();return false;" > employee info </a>
with the function
与功能
function RedirectURL()
{
window.location= createDynamicURL();
}
Hope I helped!
希望我有所帮助!