选中/取消选中某些事件的复选框
Check/Uncheck of check-boxes upon certain events
这是我的代码:script.js
$(document).ready(function() {
$('.checkbox_servers_list').click(function(){
var checkedValues = $('.group-list:checkbox:checked').map(function() {
return this.value;
}).get();
console.log(checkedValues);
var check_hosts = ["192.168.33.50", "192.168.33.100"]; // This value will be dynamic via ajax call.
for (each in check_hosts){
$(".host-list:checkbox[value='"+check_hosts[each]+"']").attr("checked", true);
}
});
});
以及HTML文件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="group-list-id" class="checkbox_servers_list"><div class="col-md-12">
<input type="checkbox" value="all" name="checkbox" class="group-list" id="group-checkbox_0">
<label for="group-checkbox_0">all</label>
</div> <div class="col-md-12">
<input type="checkbox" value="mumbai" name="checkbox" class="group-list" id="group-checkbox_1">
<label for="group-checkbox_1">mumbai</label>
</div> <div class="col-md-12">
<input type="checkbox" value="okhla" name="checkbox" class="group-list" id="group-checkbox_2">
<label for="group-checkbox_2">okhla</label>
</div> <div class="col-md-12">
<input type="checkbox" value="ungrouped" name="checkbox" class="group-list" id="group-checkbox_3">
<label for="group-checkbox_3">ungrouped</label>
</div> <div class="col-md-12">
<input type="checkbox" value="vagrant1" name="checkbox" class="group-list" id="group-checkbox_4">
<label for="group-checkbox_4">vagrant1</label>
</div>
<div class="col-md-12">
<input type="checkbox" value="bangalore" name="checkbox" class="group-list" id="group-checkbox_5">
<label for="group-checkbox_5">bangalore</label>
</div>
<div class="col-md-12">
<input type="checkbox" value="vagrant2" name="checkbox" class="group-list" id="group-checkbox_6">
<label for="group-checkbox_6">vagrant2</label>
</div>
</div>
<hr>
<div id="host-list" class="checkbox_hosts_list">
<div class="col-md-12">
<input type="checkbox" value="192.168.33.50" name="host-checkbox" class="host-list" id="host-checkbox_0">
<label for="host-checkbox_0">192.168.33.50</label>
</div>
<div class="col-md-12">
<input type="checkbox" value="192.168.33.10" name="host-checkbox" class="host-list" id="host-checkbox_1">
<label for="host-checkbox_1">192.168.33.10</label>
</div>
<div class="col-md-12">
<input type="checkbox" value="192.168.1.57" name="host-checkbox" class="host-list" id="host-checkbox_2">
<label for="host-checkbox_2">192.168.1.57</label>
</div>
<div class="col-md-12">
<input type="checkbox" value="192.168.1.59" name="host-checkbox" class="host-list" id="host-checkbox_3">
<label for="host-checkbox_3">192.168.1.59</label>
</div>
<div class="col-md-12">
<input type="checkbox" value="192.168.33.100" name="host-checkbox" class="host-list" id="host-checkbox_4">
<label for="host-checkbox_4">192.168.33.100</label>
</div>
<div class="col-md-12">
<input type="checkbox" value="192.168.1.58" name="host-checkbox" class="host-list" id="host-checkbox_5">
<label for="host-checkbox_5">192.168.1.58</label>
</div>
</div>
</body>
</html>
我试图实现的是,当我点击顶部的任何复选框时,底部的各个复选框都应该被选中(这正在发生),但当我取消选中顶部的复选框时时,底部相应的复选框应该被取消选中(这没有发生)。
我试着在js函数的开头添加这个。
$(".host-list:checkbox").attr("checked", false);
and
$(".host-list").prop("checked", false);
但是它甚至停止第一特征的执行。
第S页:演示正在工作的内容。
由于您有一个要检查的列表项,因此首先将所有项标记为未检查的
$(document).ready(function() {
$('.checkbox_servers_list').click(function(){
var checkedValues = $('.group-list:checkbox:checked').map(function() {
return this.value;
}).get();
console.log(checkedValues);
var check_hosts = ["192.168.33.50", "192.168.33.100"]; // This value will be dynamic via ajax call.
$(".host-list:checkbox").prop("checked", false);
for (each in check_hosts){
$(".host-list:checkbox[value='"+check_hosts[each]+"']").prop("checked", true);
}
});
});
尝试取消选中:
$('.host-list').find('input').prop('checked',false);
更新:你应该检查你的点击函数,然后获取值,所以我在里面添加了一个条件。
for (each in check_hosts) {
if($(".host-list:checkbox[value='" + check_hosts[each] + "']").attr("checked")){
$(".host-list:checkbox[value='" + check_hosts[each] + "']").attr("checked", false);
}else {
$(".host-list:checkbox[value='" + check_hosts[each] + "']").attr("checked", true);
}
}
相关文章:
- 如果选中了多个复选框,如何添加事件
- 复选框,然后单击事件处理
- Firefox:点击并更改未附加到文档树中的复选框元素上的事件
- 选中javascript中的属性集时,复选框更改事件未触发
- jQuery复选框(在ul>li下)检查事件
- 如何禁用自定义复选框的单击事件
- 将函数事件绑定到更改函数的复选框/标签
- 当通过选择页面上所有复选框的 jQuery 脚本选中复选框时,如何触发自定义 javascript 事件
- 选中/取消选中某些事件的复选框
- Javascript,表单中任何复选框的事件处理程序
- Jquery复选框更改事件未激发
- jQuery复选框和span共享一个单击事件
- 带有复选框的jQuery更改事件处理程序
- 复选框上的点击捕捉事件
- 使用 JavaScript 更改复选框 OnClick 事件的值
- 空格键触发单击事件复选框
- 更改 Chrome 扩展程序中未触发的事件复选框
- 在AJAX插入的动态创建元素上绑定事件(复选框)
- 单击“事件”复选框将不会触发
- 动态添加点击事件复选框,只适用于第一个复选框JQUERY