Javascript 返回时,复选框未取消选中
我有下面的代码,其中我在url中附加了带有哈希的复选框值,但是当我从那里返回时,复选框没有被取消选中。请检查下面的代码Javascript 返回时,复选框未取消选中,javascript,php,jquery,html,checkbox,Javascript,Php,Jquery,Html,Checkbox,我有下面的代码,其中我在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="ve
<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>
我有一辆自行车
我有一辆车
$checkbox=$('input[type=checkbox');
$checkbox.change(函数(){
window.location.hash='check='+$checkbox.filter(':checked').map(函数(){
返回此.value;
}).get().join(“,”);
console.log(window.location.hash);
});
返回后,我应该在url中更改哪些内容以取消选中复选框…您要查找的是
$(window).on("hashchange", function() {
// code here
}
但是,编写代码时不清除每次哈希更改上的复选框可能会很棘手。按如下方式更改代码:
$checkboxes = $('input[type="checkbox"]');
注意:这是另一个答案:http://stackoverflow.com/questions/10466648/
我有一辆自行车
我有一辆车
因此,如果表单定义具有以下属性:name=“myform”
然后可以在javascript中执行以下操作以取消选中复选框:
var服务=文件.表格['myform']['Vehicles[]'];
对于(变量i=0;i
仅在该行中进行更改…刷新代码将是相同的。如果您通过“tidy”运行代码?为了提供帮助,标记必须与标记配对,或在标记内容末尾使用“/>”自动关闭。另一个细节,需要关闭(或(更好地写为)
。输入标记没有文本组件,因此请使用标签标记。不幸的是,大多数浏览器猜测需要什么,而不是遵守html规则。两个输入类型=复选框都具有相同的名称=“车辆”。这些不是单选按钮,因此除非名称是数组,否则所有名称都应该是唯一的。
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;
}