If radio button is check display a textbox Jquery

前端 未结 5 676
北海茫月
北海茫月 2021-01-28 03:08

Hey Ya\'ll I have got a question with my radio buttons I have these 3 buttons

No  
         


        
相关标签:
5条回答
  • 2021-01-28 03:26
    $('input[name=answer]').change(function(){
        if(this.value) == 'yes'{
            $('#otherAnswer').show();
        }else{
            $('#otherAnswer').hide();
        }
    })
    
    0 讨论(0)
  • 2021-01-28 03:37

    While the answers above are sufficient if you're keen on using jquery/ javascript, this can be very easily achieved using just CSS. Try it on Codepen.

    #otherAnswer {
        display: none;
    }
    #other:checked ~ #otherAnswer {
        display: flex;
    }
    <div>
    No<input type="radio" id="no" name="answer" checked="checked" value="no"/>
    
    Yes<input type="radio" id="yes" name="answer" value="yes"/>
    
    
    Other<input type="radio" id="other" name="answer" value="other"/> 
            
    
    <input type="text" id="otherAnswer" name="otherAnswer"/>
    </div>

    0 讨论(0)
  • 2021-01-28 03:38
    $(":radio").on('click',function (){
    
    if ($(this).is(":checked") && $(this).val()=='other') ) $('#otherAnswer').show(); else $('#otherAnswer').hide();
    
    });
    
    0 讨论(0)
  • 2021-01-28 03:39

    I think this is more effective.

    $("input[name='answer']").change(function(){
    
        if($(this).val() == "yes")
        {
            $("#otherAnswer").show();
        }else{
            $("#otherAnswer").hide();
        }
    });
    
    0 讨论(0)
  • 2021-01-28 03:49
    $("input[type='radio']").change(function(){
    
       if($(this).val()=="other")
       {
          $("#otherAnswer").show();
       }
       else
       {
           $("#otherAnswer").hide(); 
       }
    
    });
    

    Here is the working example : http://jsfiddle.net/Wc2GS/8/

    0 讨论(0)
提交回复
热议问题