问题
Hey Ya'll I have got a question with my radio buttons I have these 3 buttons
No <input type="radio" name="answer" checked="checked" value="no"/>
Yes<input type="radio" name="answer" value="yes"/>
Other <input type="radio" name="answer" value="other"/>
I also have this text box
<input style="display:none;" type="text" name="otherAnswer" id="otherAnswer"/>
if the user selects the radio button with the value of "other" then display the textbox, if its anything else dont display the textbox.
I am fairly new to Jquery and I have looked up syntax for this but its all greek to me. If anyone can point me in the right direction that would be awesome!
回答1:
$("input[type='radio']").change(function(){
if($(this).val()=="other")
{
$("#otherAnswer").show();
}
else
{
$("#otherAnswer").hide();
}
});
Here is the working example : http://jsfiddle.net/Wc2GS/8/
回答2:
$(":radio").on('click',function (){
if ($(this).is(":checked") && $(this).val()=='other') ) $('#otherAnswer').show(); else $('#otherAnswer').hide();
});
回答3:
I think this is more effective.
$("input[name='answer']").change(function(){
if($(this).val() == "yes")
{
$("#otherAnswer").show();
}else{
$("#otherAnswer").hide();
}
});
回答4:
$('input[name=answer]').change(function(){
if(this.value) == 'yes'{
$('#otherAnswer').show();
}else{
$('#otherAnswer').hide();
}
})
来源:https://stackoverflow.com/questions/9569796/if-radio-button-is-check-display-a-textbox-jquery