jquery:取决于所选链接的$ .post的值

时间:2016-11-11 20:00:27

标签: javascript jquery

我的应用程序中有一个页面,有两个超链接。这两个超链接都将用户重定向到可以为新帐户添加信息的同一页面。选择链接#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>

3 个答案:

答案 0 :(得分:0)

在检测到点击操作并更改href后,您应该定义一个新的属性,例如数据ID,您的html元素。

<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>

答案 1 :(得分: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>

答案 2 :(得分:0)

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

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

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

在接收页面上,您可以使用以下功能检查传递了哪个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的值。

here

获得该功能