2011-09-22 50 views
0

我的html:jQuery的迭代一些DIV

<div class="box"> 
    <input name="radio-1" value="1" type="radio" class="radio"> 
    <div class="hidden_box"> 
    some content 
    </div> 
</div> 

<div class="box"> 
    <input name="radio-2" value="1" type="radio" class="radio"> 
    <div class="hidden_box"> 
    some content 
    </div> 
</div> 

....等等

当我在单选按钮点击我需要让 “hidden_​​box” 可见这个 “盒子” 的div。我该怎么做?

请帮助。

+0

有无你到目前为止尝试过吗? –

回答

2

既然你标记与jQuery你的Q,我将使用:

$('.radio').click(function(){ 
    $(this).parent().find('.hidden_box').show(); 
}); 

见工作example

UPDATE:如果你想这适用于所有现在和未来 '.box的' 项目,用途:

$('.radio').live('click', function(){ 
     $(this).parent().find('.hidden_box').show(); 
}); 
+0

我按照您的要求添加了更多的'.box'元素,从而更新了工作答案。 – ampersand

0
$(".radio") //select the radio buttons 
      .click(function() { //Assign a click handler 
       $(this).next().show(); //Get the element that comes after this one and make it visible 
      }); 
0
$('.radio').click(function(i,v){ 
    $(this).next('.hidden_box').toggle(); 
}); 
0
$('.radio').click(function(){ 
    $('.hidden_box', $(this).parent()).show(); 
}) 

会的工作,即使hidden_​​box是不是下一个DOM元素。 jquery选择器中的第二个参数是范围。

更新:使用find(),如在其他地方的答案证明看起来有点吸尘器我

+0

看起来很漂亮,但不幸的是它不起作用。 – Alex

+0

是的,哎呀,与复选框混淆 – CambridgeMike

+0

如果你用'$(this)'替换了'this'这两个事件,原来的方法也会起作用。 – Stephan