If radio button is check display a textbox Jquery

僤鯓⒐⒋嵵緔 提交于 2019-12-20 06:46:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!