Removing Check on Radio Button jQuery
I am using:
var choice;
$('a.enter').click(function() {
choice = $('input:checked').val();
});
To determine choice for a radio button being checked.
Then, when running an event like:
$('a.exit').click(function() {
$('input').prop('checked', false);
});
I clear out the check.. Then when I run the first event again, instead of choice equaling the value of the checked radio button, it 开发者_JAVA百科returns "undefined"
Any ideas?
Thanks!
I may not be totally clear on the process you're describing, but if I understand correctly, when you:
run the first event again
$('input:checked').val();
will return 'undefined' since there are no checked inputs - you just unchecked them all with $('input').prop('checked', false);
.
What result are you trying to achieve?
After you clicked on a.exit
$('input:checked') return jQuery object without elements.
.val()
for jQuery object without elements always return undefined
because can't find property value
$('a.enter').click(function() {
choice = $('input:checked').val();
});
choise is not a GLOBAL variable here.
Make some demo and show the url. Or a simple jsfiddle, please.
EDIT:
It seems I intentionally skipped the top declaration to let some users make polemic comments.
Actually it doesn't matter you declare it or not.
When the second time you click a.enter, there is no checked inputs, and no value is assigned to your variable.
Use this code
http://jsfiddle.net/ZE2Pb/
精彩评论