Reputation: 706
$('form').change(function() {
if('input[type=radio]:checked') {
alert('Already Selected');
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method='get' action=''>
<input type="radio" name="radiobutton">
<input type="radio" name="radiobutton">
<input type="radio" name="radiobutton">
</form>
In this form
here, I'm trying to prevent the user from clicking twice on selected radio
button, So i want to alert
a message everytime he clicks the selected radio
button.
Upvotes: 0
Views: 588
Reputation:
So a few things based on your plain english explanation:
$('input[type=radio]').click(function() {
if($(this).data('previously-checked')) {
alert('Already Selected');
}
// update all as not clicked
$('input[type=radio]').data('previously-checked', false);
// mark current one as checked
$(this).data('previously-checked', true);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method='get' action=''>
<input type="radio" name="radiobutton" value="Y">
<input type="radio" name="radiobutton" value="Y">
<input type="radio" name="radiobutton" value="Y">
</form>
Upvotes: 1