选择和取消选择所有复选框
问题描述:
我无法实现这些事情,在此页面中,我想实现以下三件事:
I'm not able to implement these things,In this page I want to do implement 3 things:
1> 默认情况下,此页面将选中Apple和Cat. 2> 对于Startall,将检查并禁用所有功能. 3> 对于Stopall,将禁用所有功能以及默认的(AC)值.*
1> By default Apple and Cat will be checked in this page. 2> For Startall all the features will be checked and disabled. 3> For Stopall, all the features will be disabled as well as default(AC) value will be disabled.*
<script type="text/javascript"> //This jquery is using for on selecting the checkbox,it will assign the text field of policyName and features
$(document).ready(function() {
$('.check').click(function(){
$("#policyName").val('Start');
$("#features").val('');
$(".check").each(function(){
if($(this).prop('checked')){
$("#policyName").val($("#policyName").val() + $(this).val());
$("#features").val($("#features").val() + $(this).data('name'));
}
});
});
});
</script>
这是我的jsp页面:
<div align="center" id="checkboxes">
<input type="checkbox" name="startall" data-name="Startall" class="check" id="check" value="all"> All
<input type="checkbox" name="stopall" data-name="Stopall" class="check" id="check" value="stopall"> STOPall
<input type="checkbox" name="apple" data-name="Apple" class="check" id="check" value="a"> Apple
<input type="checkbox" name="ball" data-name="Ball" disabled="disabled" checked="checked" id="check" value="b"> Ball
<input type="checkbox" name="cat" data-name="Cat" class="check" id="check" value="a"> Cat
<input type="checkbox" name="dog" data-name="Dog" checked="checked" disabled="disabled" id="check" value="d"> Dog
<input type="checkbox" name="elephant" data-name="Elephant" disabled="disabled" checked="checked" id="check" value="e">
Elephant
</div>
任何帮助....将不胜感激.
Any Assistance....will be apprciated.
答
尝试一下
ID 应该是唯一的.尝试改用其他ID或类.
ID in your HTML should be unique.. Try using different id's or classes instead.. I have written the code using the name attribute which is a slow selector..
$(document).ready(function() {
$('[name="apple"], [name="cat"]').prop('checked', true);
$('[name="startall"]').on('click', function() {
var $checkboxes = $('input[type="checkbox"]').not('[name="startall"], [name="stopall"]');
if (this.checked) {
$checkboxes.prop({
checked: true,
disabled: false
});
$('#textbox').val( $(this).attr('data-name'));
}
else{
$checkboxes.prop({
checked: false
});
$('#textbox').val('');
}
});
$('[name="stopall"]').on('click', function() {
var $checkboxes = $('input[type="checkbox"]').not('[name="startall"], [name="stopall"]');
if (this.checked) {
$checkboxes.prop({
checked: false,
disabled: true
});
$('#textbox').val( $(this).attr('data-name'));
}
else{
$checkboxes.prop({
disabled: false
});
$('#textbox').val('');
}
});
});
UPDATED DEMO