2012-03-05 66 views
0

嘿你们大家我有一个问题,我的收音机按钮,我有这3个按钮如果单选按钮是检查显示一个文本框的Jquery

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"/> 

如果用户选择值为“其他”的单选按钮,则显示该文本框,如果其他东西不显示该文本框。

我对Jquery来说相当新,我查了这个语法,但是它对我来说都是希腊语。如果任何人都可以指引我,那将是非常棒的方向!

回答

4
$("input[type='radio']").change(function(){ 

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

}); 

这里是工作示例:http://jsfiddle.net/Wc2GS/8/

+0

太棒了...这个完美的作品,谢谢Shyju – user979331 2012-03-05 16:13:24

+0

@ user1193385:欢迎。很高兴知道它的工作。 – Shyju 2012-03-05 16:18:00

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

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

}); 
+0

为什么不使用'this.checked'和'this.value'而不是不必要地创建jQuery对象和调用函数? – 2012-03-05 16:05:48

+0

@AnthonyGrist好吧。但因为我通常在我自己的代码中使用我的jquery对象 - 即时自动执行它......虽然你是对的。你可以去查询里面的对象。 – 2012-03-05 16:07:07

0
$('input[name=answer]').change(function(){ 
    if(this.value) == 'yes'{ 
     $('#otherAnswer').show(); 
    }else{ 
     $('#otherAnswer').hide(); 
    } 
}) 
+0

这不会做他们需要的东西;有三个单选按钮'name =“answer”',其中两个应确保'input'被隐藏。 – 2012-03-05 16:08:37

+0

嘿,你是对的! – 2012-03-05 16:09:21

0

我觉得这是更有效的。

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

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