在返回导航时,复选框没有被选中


On navigating back,checkbox not getting unchecked

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

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

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

将代码修改如下:

$checkboxes = $('input[type="checkbox"]');
Note: This is taken from another answer: http://stackoverflow.com/questions/10466648/
<label>
    <input type="checkbox" name="Vehicles[]" value="Bike"> I have a bike
</label>
<br />
<label>
    <input type="checkbox" name="Vehicles[]" value="Car"> I have a car
</label>
<br /> 
So if your form definition has the attribute: name="myform"
then you can do the following in javascript to uncheck the checkboxes:
var services = ​document.forms['myform']['Vehicles​[]']​;
for (var i = 0; i < Vehicles.length; i++) 
{
    Vehicles[i].checked = false;
}