2013-06-26 66 views
-1

在我的ASP MVC 3視圖中,當頁面加載時,我有一些fieldset元素,它們是hidden。基於用戶選擇一組單選按鈕,我需要使相應的fieldset可見。抓住div內的所有fieldset元素

我想要在jquery中做一個fieldset元素的數組,然後遍歷它們,調整它們的visibility屬性(如果它們與所選單選按鈕相匹配或不匹配)。這可能嗎?

由於fieldsets中有這麼多的代碼,我附上了下面的屏幕截圖以節省空間/使其更具可讀性。 fieldsets我想改變的是在RightDiv裏面。如果您需要更多的細節,請告訴我。 THX enter image description here

+0

難道你不能提供一個最小的工作示例,而不是張貼圖片? –

+0

正如我所說,我沒有發佈所有的代碼來節省空間。它無法看到每一個'fieldset'。 – NealR

回答

0

對不起,在這發佈了一下。試了下面的以下,它工作得很好。

$(document).ready(function() { 
    $('input[name=TransactionType]').change(function() { 
     var radioValue = $(this); 
     var elements = []; 

     $('#RightDiv').children().each(function() { 
      elements.push($(this)); 
     }); 
    }); 
}); 
1

你可以試試這個:

$(function(){ 
    $('[name="TransactionType"]').change(function(){ 
     var id = '#' + this.className; //Get the id from the clicked radio classname 
     $('#RightDiv').find('fieldset').hide();// hide all fieldsets; 
     $('#RightDiv').find(id).show(); // show the selected one. 
    }); 
}); 

只是注意到,在您的HTML幫助您提供的第一個過載爲所有相同的名稱。一切都很好,除非我相信它會爲每一個創建重複的ID。您可能想要在HTML屬性中覆蓋它。

@Html.RadioButton("TransactionType", false, new{@class="Enroll", id="Radio1"}) 
@Html.RadioButton("TransactionType", false, new{@class="New", id="Radio2"})