如何通过javascript中的按钮点击来选择和取消选择多个复选框
How to select and deselect multiple check box with a button click in javascript
在我的项目中,我有多个复选框(共27个)。我试图通过单击一个按钮来选择所有复选框,我可以做到。但我想通过单击相同的按钮来取消选择相同的复选框。所以按钮应该像选择器和取消选择器一样。我不能用复选框代替按钮。我的html代码是:
<div class="countryAll">
<span class="countrySelect_lngTrans_Translatable" title="Select All">
<input type="button" class="selectAllcountry" />
</span>
<span class="displayData">Select whole country</span>
</div>
js:
$(self.element).on('click', '.selectAllcountry', function(e) {
debugger;
$('.chkCountry').trigger('click')
e.stopPropagation();
});
使用trigger
将始终切换状态,因此,如果某些元素被检查,而某些元素未被检查,则它将无法正常工作。
相反,你可以尝试类似的东西
$(document).on('click', '.selectAllcountry', function(e) {
var $checks = $('.chkCountry');
$checks.prop('checked', !$checks.is(':checked'))
e.stopPropagation();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="countryAll">
<span class="countrySelect_lngTrans_Translatable" title="Select All">
<input type="button" class="selectAllcountry" />
</span>
<span class="displayData">Select whole country</span>
</div>
<input class="chkCountry" type="checkbox" />
<input class="chkCountry" type="checkbox" />
<input class="chkCountry" type="checkbox" />
<input class="chkCountry" type="checkbox" />
试试这个
$('.selectAllcountry').on('click', function(){
if ($('.selectAllcountry').is(':checked')) {
$('.chkCountry').find(':checkbox').prop('checked', true);
} else {
$('.chkCountry').find(':checkbox').prop('checked', false);
}
});
试试这个。。。。
$(document).on('click', 'input.selectAllcountry', function(e) {
if ($('input.selectAllcountry').is(':checked'))
{
$('.chkCountry').prop('checked',true);
}
else
$('.chkCountry').prop('checked',false);
});
var chkStatus = true;
$('.selectAllcountry').click(function() {
$('.countryAll').find(':checkbox').prop('checked', chkStatus);
chkStatus = !chkStatus;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="countryAll">
<span class="countrySelect_lngTrans_Translatable" title="Select All">
<input type="button" class="selectAllcountry" />
</span>
<span class="displayData">Select whole country</span>
<br>
<br>
<input type="checkbox" name="vehicle" value="Bike">I have a bike
<br>
<input type="checkbox" name="vehicle" value="Bike">I have a bike
<br>
<input type="checkbox" name="vehicle" value="Bike">I have a bike
<br>
<input type="checkbox" name="vehicle" value="Bike">I have a bike
<br>
<input type="checkbox" name="vehicle" value="Bike">I have a bike
<br>
<input type="checkbox" name="vehicle" value="Bike">I have a bike
<br>
<input type="checkbox" name="vehicle" value="Bike">I have a bike
<br>
<input type="checkbox" name="vehicle" value="Bike">I have a bike
<br>
</div>
在完成选择部分后,现在将重点放在取消选择上
一次删除所有复选框输入
$('input:checkbox').removeAttr('checked');
也可以添加类作为标识符,然后根据类删除选中的元素
例如。
$('.chkCountry').removeAttr('checked');
我认为您在代码中犯了一个错误——在html标记中使用了class属性twise。我认为这就是为什么它采用第一类属性而忽略其他属性的原因。
<div class="countryAll">
<span class="countrySelect_lngTrans_Translatable" title="Select All">
<input type="button" class="selectAllcountry" />
</span>
<span class="displayData">Select whole country</span>
</div>
请将所有类组合在一个类属性中。我认为这会有帮助。
相关文章:
- 通过在d3中单击来选择/取消选择
- 复选框选择/取消选择触发器第一次工作,但在随后的尝试中失败
- 选择/取消选择复选框,该复选框未按预期使用.coneast('table').find('tbo
- 在angular.js中选择取消选择树结构
- Javascript函数用于选择/取消选中radgrid标头模板中的所有复选框
- 一个变量,多个复选框选择/取消选择 jQuery
- 基于下拉选择的单选选择/取消选择
- 选择/取消选择花式树插件上所有具有相同ID的节点
- 选择2 取消选择所有值
- 使用 JQuery 多次选择取消选中复选框
- jQuery:需要知道在变更事件中选择/取消选择了哪个选项
- 带有选择/取消选择和汇总选项的表格
- 在按钮上切换类(选择/取消选择)
- 关闭浏览器选项卡时,如何在确认框中默认选择“取消”
- jqxTreeGrid如果我单击一行中的特定单元格,则禁用对该行的选择/取消选择
- 如何使用 JavaScript/jQuery 选择/取消选中复选框
- 即使用户选择“取消”,我如何保留FileDialog输入值?(ASP.Net/Javascript)
- 使用JQuery取消选择/取消聚集输入字段
- 如何将JavaScript应用于引导程序btn组以选择/取消选择子按钮元素
- 如何选择/取消选择所有克隆节点