如果选中单选按钮,则选中显示文本框Jquery

如果选中单选按钮,则选中显示文本框Jquery,jquery,radio-button,Jquery,Radio Button,嘿,我有一个关于单选按钮的问题我有这3个按钮 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"/> 否 对 其他 我也有这个文本框 <input styl

嘿,我有一个关于单选按钮的问题我有这3个按钮

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"/>
否
对
其他
我也有这个文本框

<input style="display:none;" type="text" name="otherAnswer" id="otherAnswer"/>

如果用户选择值为“other”的单选按钮,则显示文本框,如果有其他内容,则不显示文本框

我对Jquery是相当陌生的,我已经查找了它的语法,但对我来说它完全是希腊语。如果有人能给我指出正确的方向,那就太棒了

$(":radio").on('click',function (){

if ($(this).is(":checked") && $(this).val()=='other') ) $('#otherAnswer').show(); else $('#otherAnswer').hide();

});

下面是一个工作示例:

我认为这更有效

$("input[name='answer']").change(function(){

    if($(this).val() == "yes")
    {
        $("#otherAnswer").show();
    }else{
        $("#otherAnswer").hide();
    }
});

如果您热衷于使用jquery/javascript,上面的答案就足够了,但这可以很容易地通过CSS实现。试穿一下

#其他答案{
显示:无;
}
#其他:选中~#其他答案{
显示器:flex;
}

不
对
其他

为什么不使用
this.checked
this.value
而不是不必要地创建jQuery对象和调用函数?@AnthonyGrist其确定。但由于我通常在自己的代码中使用jquery对象,所以我会自动执行它。。。虽然你是对的。您可以对内部对象进行反jquery操作,这不会满足他们的需要;有三个单选按钮带有
name=“answer”
,其中两个按钮应确保
输入被隐藏。
$("input[type='radio']").change(function(){

   if($(this).val()=="other")
   {
      $("#otherAnswer").show();
   }
   else
   {
       $("#otherAnswer").hide(); 
   }

});
$("input[name='answer']").change(function(){

    if($(this).val() == "yes")
    {
        $("#otherAnswer").show();
    }else{
        $("#otherAnswer").hide();
    }
});