javascript - 返回时,复选框未取消选中

标签 javascript php jquery html checkbox

我有以下代码,其中我在网址中附加带有哈希值的复选框值,但是当我从那里导航回来时,复选框没有被选中。请检查下面的代码

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>


<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car"> I have a car<br>

<script>
$checkboxes = $('input[type=checkbox');
$checkboxes.change(function(){
    window.location.hash = 'check=' + $checkboxes.filter(':checked').map(function(){
        return this.value;   
    }).get().join(",");
    console.log(window.location.hash);
});
</script>
</body>
</html>

我应该在 url 中更改什么,以便在返回后取消选中复选框...

最佳答案

您要找的是

$(window).on("hashchange", function() {
    // code here
}

但是,编写不清除每次哈希更改时的复选框的代码可能会很棘手。

关于javascript - 返回时,复选框未取消选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23926648/

相关文章:

javascript - 带有 jsonp 的 jQuery .ajax() 不调用成功回调函数

javascript - 如何让我的动画技能栏响应

javascript - 将 Go 函数传递给 html/js 按钮 "onclick"响应

javascript - 为什么我的输入字段需要显示错误作为初始状态?

php - 自动将新条目添加到表 codeigniter 中

php - MySQL/PHP "data too long for column"

JavaScript xml 查询

javascript - 如何在 ASP.NET MVC 中从 JSON 呈现 FullCalendar 中的事件?

php - Facebook 共享器弹出窗口

jquery - 如何在Jquery中找到复选框组