0

我试图通过id从 url 解析到锚标记来传递我得到的。下面是我的代码,它将id通过从 url 解析它来获得。

让我们举个例子,假设如果如果id is 123那么我的锚标签应该是这样的 -

<a href="some_url&userId=123"> something here </a>

但不知何故,使用下面的代码它不起作用。谁能解释我做错了什么。

<body>
<script>

var id = getUrlParameters()["ID"];    
id = unescape(id);


</script>

// Below things doesn't work with the way I am doing.
<a href="some_url&userId=id"></a>

</body>

我无法成功地将 id 传递给锚标记。我在这里做错了什么?

更新代码:-

实际的锚标签是这样的——

<a href="https://sox.host.net:7020/ps/sox/HRS_HRAM.JN_HRS_CE.GBL&userId=id"></a>

现在我需要用基础替换id锚标记,无论它的值是什么。123id

更新的问题:-

我正在尝试将变量值传递给锚标记。我有一个 Javascript 脚本,可以从 url 获取值。所以假设在下面的代码中,

value of variable id is 54321

那么我的锚标签应该是这样的——

<a href="some_url&jobId=54321"><img src="url" style="border:none" onmouseover="this.src='url'" onmouseout="this.src='url'"/></a>    

但是假设value of variable id is 2612, 那么我的锚标签应该是这样的——

<a href="some_url&jobId=2612"><img src="url" style="border:none" onmouseover="this.src='url'" onmouseout="this.src='url'"/></a> 

使用下面的代码,我有,它不适合我。我无法成功地将 id 变量值传递给锚标记。

<body>
<script>
function getParameters() {

 // some code

}

var id = getParameters()["ID"];    
id = unescape(id);

// some code

</script>

<a href="some_url&jobId=id"><img src="url" style="border:none" onmouseover="this.src='url'" onmouseout="this.src='url'"/></a>

</body>
4

3 回答 3

1

您尝试使用&而不是?哪个会导致错误。

尝试这个:

<a href="some_url?userId=123"> something here </a>

解释

我所做的只是将 更改&?.

记住这两条规则:

  • 您应该使用该?符号将 GET 值连接到 url
  • 您应该使用该&符号将 GET 值相互连接

我希望这对您有所帮助,并随时寻求进一步的帮助!

于 2013-07-29T23:28:19.897 回答
0

The parameter you are sending is called userId and the one you try to retrieve is ID. You should use var id = getUrlParameters()["userId"] instead.

EDIT: If your problem is just passing id as an argument to the href, try this:

First, add a tag id to you link tag:

<a href="some_url&userId=id" id="a_tag_id_here">...</a>

Then, add the href with JQuery, using your id variable:

$('a_tag_id_here').attr('href','https://sox.host.net:7020/ps/sox/HRS_HRAM.JN_HRS_CE.GBL&userId='+id);

It's up to you to decide what should trigger this command. If you want it to be set right after the loading of the page, just put this code inside this:

$(document).ready(function() {
  // code here
});
于 2013-07-29T23:01:59.043 回答
0

尝试这个,

var getid= /*your id*/

然后使用方法从锚标记获取 url.attr('href')替换 = 之后的部分,使用split('=')方法。

外汇:

var temp=$('a').attr('href').split('=')[0];
$('a').attr('href',temp+'='+getid);
于 2013-07-31T06:32:31.090 回答