jQuery - if else statement regarding radio buttons
I have 2 radio buttons for yes and no, i was wondering if it is possible to run an if else statement in jQuery to see if one or the other is pressed?
<input type="radio" name="option" value="yes">
<input type="radio" name="option" value="no">
if yes is selected then do this else if no开发者_运维知识库 is selected do something else
Is that possible in jQuery?
You can write
if ($('input:radio[name="option"]:checked').val() === "yes")
However, you should use a single checkbox instead.
try
if ( $('[type=radio]').filter(':checked').length)
After checkign Slaks answer, and re-reading the question
var result = $('input:radio[name="option"]:checked').val();
if (result === 'yes') {
}else if (result === 'no') {
}
that way you aren't running to same query twice or duplicating code
精彩评论