如何在页面刷新时保持链接状态

时间:2016-12-23 23:40:35

标签: javascript jquery html

我有一个包含4个可点击(href)链接列表的页面。 每个链接都指向同一个表单提交页面。 用户提交表单并返回原始页面,他们点击的链接现已禁用,剩下3个可点击链接。 可以重复此过程,直到没有可点击的链接为止。 因此,每个链接只能被点击一次。 到现在为止还挺好... 但是,如果刷新页面,则会再次启用所有已禁用的链接。 “我怎样才能防止这种情况发生”。 每个链接只应单击一次,即使刷新页面也是如此。 我该怎么办? 这是我的HTML代码......

<script type="text/javascript">
    function check(link) {
        $(link).replaceWith($(link).text());
    }
</script>
<style>

    a:link{
        color:#1DAAA1;
        text-decoration:none;

    }
    a:visited{
        color:#1DAAA1;
    }
    a:hover{
        background-color: #ff7f00;
        color: #FFF;
    }

</style>

</head>
<body style="background-color:#eeeeee">

    <table style="margin-top:200px; width:500px; background-color:#ffffff" align="center" border="5px solid">
        <tr align="center" >
            <td><a href="submitform.php" target="_blank" onclick="return check(this);"> Submission 1 </a></td>
            <td><a href="submitform.php" target="_blank" onclick="return check(this);"> Submission 2 </a></td>
            <td><a href="submitform.php" target="_blank" onclick="return check(this);"> Submission 3 </a></td>
            <td><a href="submitform.php" target="_blank" onclick="return check(this);"> Submission 4 </a></td>
        </tr>

    </table>
</body>

1 个答案:

答案 0 :(得分:0)

这是解决问题的一种方法。

<form action="submitform.php" method="post" name="yourform">
<table>
    <tr style="text-align:center;" >
        <td><a <?php echo $_POST["sub1"] ? "" : "href='#'"?> onclick="document.forms["yourform"].submit()" name="sub1"> Submission 1 </a></td>
        <td><a <?php echo $_POST["sub2"] ? "" : "href='#'"?> onclick="document.forms["yourform"].submit()" name="sub2"> Submission 2 </a></td>
        <td><a <?php echo $_POST["sub3"] ? "" : "href='#'"?> onclick="document.forms["yourform"].submit()" name="sub3"> Submission 3 </a></td>
        <td><a <?php echo $_POST["sub4"] ? "" : "href='#'"?> onclick="document.forms["yourform"].submit()" name="sub4"> Submission 4 </a></td>
    </tr>
</table>
</form>

您还可以禁用链接并将值存储在变量中。

<a href onclick="storeVar" title="sub1">Submission1</a>
<script>
var arr = [];
function storeVar(e){
    if(arr.indexOf($(e.target).attr("title")) == -1)
        arr.push($(e.target).attr("title"));
    if(arr.length > 3)
        //do something
}
</script>
相关问题