I have an asp.net webform and on one page i a CheckboxList with 7 ListItem's in it. What i want is, if the first checkbox is selected for the other 6 to be disabled and if the second, thrid etc is selected, for the first checkbox to be disabled.
Users should not be able to select checkbox 1 and a ny of the other 6 whilst its checked and same going, they should not be able to selected the first checkbox if any of the other checkboxes are selected.
The code below works in this scenario
$(document).on('click', ':checkbox', function () {
//disables all checkboxes if 1st one is checked
$(":checkbox:not('#MainContent_Services_0')").attr('disabled', $('#MainContent_Services_0').is(':checked'));
//disables 1st checkbox if any other is checked
$("#MainContent_Services_0").attr('disabled', $(":checkbox:not('#MainContent_Services_0')").is(':checked'));
})
The issue i have though is that if the user selects checkbox 1 (for example) goes away form the page and then navigates back, the selected checkbox is already checked (as expected) but all other checkboxes are enabled and shouldn't be. This is what i need
help with.
Not sure on your solution. My disable/enable is working fine when the user first hits the page and selects a checkbox. My issue is when the user navigates back to the page, they are all enabled. I want the relevent checkboxes to be disabled/enabled depending
on the selection previously made.
e.g. If the user selected the first one, then all the other checkboxes should be disabled
My script is only firing when i select a checkbox, so users are able to select one of the other 6 BUT they then disable. Checkboxes 2-6 should have already been disabled to stop this from happening
The issue i have though is that if the user selects checkbox 1 (for example) goes away form the page and then navigates back, the selected checkbox is already checked (as expected) but all other checkboxes are enabled and shouldn't be.
You current code will run only when user click on the checkbox. to disable the checkboxes on pageload you need to run your jquery code in $( document ).ready() block like given below
// A $( document ).ready() block.
$( document ).ready(function() {
//disables all checkboxes if 1st one is checked
$(":checkbox:not('#MainContent_Services_0')").attr('disabled', $('#MainContent_Services_0').is(':checked'));
//disables 1st checkbox if any other is checked
$("#MainContent_Services_0").attr('disabled', $(":checkbox:not('#MainContent_Services_0')").is(':checked'));
});
Member
5 Points
45 Posts
Disable Additional Checkboxes Depending On Selection From CheckboxList
Sep 02, 2015 09:41 AM|Murday1983|LINK
I have an asp.net webform and on one page i a CheckboxList with 7 ListItem's in it. What i want is, if the first checkbox is selected for the other 6 to be disabled and if the second, thrid etc is selected, for the first checkbox to be disabled.
Users should not be able to select checkbox 1 and a ny of the other 6 whilst its checked and same going, they should not be able to selected the first checkbox if any of the other checkboxes are selected.
The code below works in this scenario
The issue i have though is that if the user selects checkbox 1 (for example) goes away form the page and then navigates back, the selected checkbox is already checked (as expected) but all other checkboxes are enabled and shouldn't be. This is what i need help with.
Contributor
3105 Points
2122 Posts
Re: Disable Additional Checkboxes Depending On Selection From CheckboxList
Sep 02, 2015 09:53 AM|Xequence|LINK
either your html has them disabled by default
or you handle it with javascript
something like this,
$(function(){
// disable all by design
}
now run your click code,
Credentials
CurbSmash
Member
5 Points
45 Posts
Re: Disable Additional Checkboxes Depending On Selection From CheckboxList
Sep 02, 2015 10:09 AM|Murday1983|LINK
Not sure on your solution. My disable/enable is working fine when the user first hits the page and selects a checkbox. My issue is when the user navigates back to the page, they are all enabled. I want the relevent checkboxes to be disabled/enabled depending on the selection previously made.
e.g. If the user selected the first one, then all the other checkboxes should be disabled
My script is only firing when i select a checkbox, so users are able to select one of the other 6 BUT they then disable. Checkboxes 2-6 should have already been disabled to stop this from happening
All-Star
50831 Points
9895 Posts
Re: Disable Additional Checkboxes Depending On Selection From CheckboxList
Sep 02, 2015 10:28 AM|A2H|LINK
You current code will run only when user click on the checkbox. to disable the checkboxes on pageload you need to run your jquery code in $( document ).ready() block like given below
Aje
My Blog | Dotnet Funda