2016-11-11 48 views
0

我有一个页面在我的应用程序中有两个超链接。这两个超链接都会将用户重定向到可以为新帐户添加信息的相同页面。当选择链接#1时,传递给控制器​​动作的一个值必须为1.如果选择了另一个链接,则值为2.这怎么用jQuery来完成?

链接:

<div style="position:absolute; width:100px; top:25%; left:50%; font-family:Arial; font-weight:bold; white-space:nowrap "> 
    <a href="~/rxcard/addaccount" style="color:#444444;">ADD CLINIC</a>  
</div> 
<div style="position:absolute; width:100px; top:33%; left:48%; font-family:Arial; font-weight:bold; white-space:nowrap"> 
    <a href="~/rxcard/addaccount" style="color:#444444;">ADD MEDICAL OFFICE</a> 
</div> 

回答

0

为什么地球上你需要使用jQuery这个?使用简单的GET参数。

<a href="~/rxcard/addaccount?type=1" style="color:#444444;">ADD MEDICAL OFFICE</a> 

注意?type=1传递一个值为1的get参数。

在接收页面上,您可以使用folllowing函数来检查传递哪个get参数。

function findGetParameter(parameterName) { 
    var result = null, 
     tmp = []; 
    location.search 
    .substr(1) 
     .split("&") 
     .forEach(function (item) { 
     tmp = item.split("="); 
     if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]); 
    }); 
    return result; 
} 

使用这样的:findGetParameter('type')得到type从URL的值。

here得知该功能

0

你应该定义后,像数据-ID的HTML元素的新属性检测到点击动作并更改HREF。

<div> 
    <a href="~/rxcard/addaccount" data-id='1'>ADD CLINIC</a>  
</div> 
<div> 
    <a href="~/rxcard/addaccount" data-id='2'>ADD MEDICAL OFFICE</a> 
</div> 

<script> 
$("a").on('click',function(){ 
    var thiz = $(this); 
    thiz.attr('href',thiz.attr('href')+?val=thiz.attr('data-id')); 
}); 
</script> 

如果你在初始化时有价值,你可以给不同的hrefs定位锚。在这种情况下,你不需要Jquery或Javascript;

<div> 
    <a href="~/rxcard/addaccount?val=1">ADD CLINIC</a>  
</div> 
<div> 
    <a href="~/rxcard/addaccount?val=2">ADD MEDICAL OFFICE</a> 
</div> 
0

只需添加一个GET参数的链接,他们将继续指向同一个URL,但也将传递一个参数

<div style="position:absolute; width:100px; top:25%; left:50%; font-family:Arial; font-weight:bold; white-space:nowrap "> 
    <a href="~/rxcard/addaccount?param=1" style="color:#444444;">ADD CLINIC</a>  
</div> 
<div style="position:absolute; width:100px; top:33%; left:48%; font-family:Arial; font-weight:bold; white-space:nowrap"> 
    <a href="~/rxcard/addaccount?param=2" style="color:#444444;">ADD MEDICAL OFFICE</a> 
</div>