I have a click listener on my radiobuttons.
I need to check whether a radiobutton was already checked prior to the click, or if it wasnt.
$('.GridRadio').live('click', function () {
alert($(this).is(':checked'));
});
However, the above alerts true every time. How can i see if it was checked already, prior to when being clicked?
You could use data()
to store the previous value of the radio, and then check the current value against that:
$(".GridRadio").click(function() {
var lastValue = $(this).data("last-value");
var value = $(this).val();
if (lastValue == value) {
alert("Already checked");
}
else {
alert("Not previously checked");
}
$(".GridRadio").data("last-value", "");
$(this).data("last-value", value);
});
If you set the checked
attribute on one of the radios in HTML on page load, add this code to set the data
for that specific one:
$(".GridRadio:checked").data("last-value", $(".GridRadio:checked").val())
If you're using live
you probably append $('.GridRadio')
after $(document).ready
. if that's right - you can do this check when you append it. then when it is being clicked - it will always be the negative value.
(and if it is in the page from the beginning - you can do this check once the page is reloaded, before someone clicks on it.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.