2016-02-04 73 views
1

如果我想創建隱藏在.radio-click-hide點擊我怎麼會遍歷做到這一點的元素.dependent-box部分事件的兄弟?我已經試過這一點,但沒有奏效:DOM遍歷找到父

jQuery(function ($) { 
    $('.radio-click-hide').on("click", function (e) { 
     $(this).closest('.dependent-box').hide() 
    }); 
}); 

另外,我有很多這樣的我的整個程序,因此需要針對最近.dependent-box

<table> 
    <tr class="form-group"> 
     <td class="form-label"> 
      @Html.LabelFor(model => model.IsAchievedByLiveMeeting, htmlAttributes: new {@class = "control-label"}) 
     </td> 
     <td class="form-input"> 
      @Html.RadioButtonFor(model => model.IsAchievedByLiveMeeting, true, new { @class = "radio-click-hide" }) Yes 
      @Html.RadioButtonFor(model => model.IsAchievedByLiveMeeting, false, new { @class = "radio-click-show" }) No 
      @Html.ValidationMessageFor(model => model.IsAchievedByLiveMeeting, "", new {@class = "text-danger"}) 
     </td> 
    </tr> 
    <tr class="form-group dependent-box"> 
     <td class="form-label"></td> 
     <td class="form-input"> 
      @Html.LabelFor(model => model.LiveMeetingExplaination, htmlAttributes: new {@class = "control-label"}) 
      @Html.EditorFor(model => model.LiveMeetingExplaination, new {htmlAttributes = new {@class = "form-control"}}) 
      @Html.ValidationMessageFor(model => model.LiveMeetingExplaination, "", new {@class = "text-danger"}) 
     </td> 
    </tr> 
</table> 

回答

2

單獨closest()將無法​​正常工作因爲.dependent-box元素是複選框的父級tr的兄弟。試試這個:

$('.radio-click-hide').click(function() { 
    $(this).closest('tr').next('.dependent-box').hide() 
});