Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  41] [ 5]  / answers: 1 / hits: 15348  / 13 Years ago, fri, august 19, 2011, 12:00:00

I am trying to prevent a user from changing the state of a radio button using jquery or javascript. I could do this with the disabled property but I don't like the grayed out look. I am showing the user a true/false state of one of my models, so I don't want them to be able to change it. There is only one radio button in the group that is either checked (true) or unchecked (false) based on the model, so they can't uncheck it but they can check and change the radio button state. The radio button doesn't get submitted in a form so it is only for reading from the database/model but it would be a bad user experience if they changed it to the opposite value that is actually in the database. Here is my latest attempt at the jquery and html structure.



<input type=radio checked=checked>


or



<input type=radio>


and the jquery



$(document).ready(function () {
$(input[type = radio]).change(function () {
$(this).removeAttr(checked);
});
});

More From » jquery

 Answers
12

Try this



$(document).ready(function () {
$('input[type = radio]').change(function () {
return false;
});
});

[#90538] Wednesday, August 17, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brandensebastiand

Total Points: 323
Total Questions: 115
Total Answers: 106

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;