jquery单选按钮显示/隐藏

问题描述:

我有3个单选按钮。
*如果单击电子邮件按钮将只显示电子邮件文本框
*如果单击集合将只显示集合文本框并隐藏其他div。我怎么做? 下面的代码似乎只显示,但不能隐藏在更改!jquery单选按钮显示/隐藏

<input type="radio" name="d_method" class="c_email"/>Email 
<input name="d_method" type="radio" class="c_collection"/>Colletion 
<input name="d_method" type="radio" class="c_post"/>Post 

和3个隐藏的div id为:

<div id="c_email" style="display:none;"> 
    email textbox 
</div> 
<div id="c_collection" style="display:none;"> 
    collection textbox 
</div> 
<div id="c_post" style="display:none;"> 
    post textbox 
</div> 

和jQuery:

$('.c_email').change(function() { 
     $('#c_email').stop().fadeIn(); 
    }, function(){ 
     $('#c_email').stop().hide(); 
    }) 
    $('.c_collection').change(function() { 
     $('#c_collection').stop().fadeIn(); 
    }, function(){ 
     $('#c_collection').stop().hide(); 
    }) 
    $('.c_post').change(function() { 
     $('#c_post').stop().fadeIn(); 
    }, function(){ 
     $('#c_post').stop().hide(); 
    }) 

你可以简单地做

$(':radio').change(function() { 
    var itemSelector = '#' + $(this).attr('class'); 
    $('div').stop().fadeOut(function() { 
     $(itemSelector).fadeIn(); 
    }); 
}); 

这一切都是基于你的约定班级名称与您的ID相同

+0

感谢它的工作;) – tonoslfx 2011-03-22 16:11:24