抓住div中的所有fieldset元素

时间:2013-06-26 16:16:34

标签: asp.net-mvc asp.net-mvc-3 jquery-ui jquery

在我的ASP MVC 3视图中,当页面加载时,我有许多fieldset元素hidden。根据用户选择的一组单选按钮,我需要使相应的fieldset可见。

我想通过创建fieldset元素的数组在jquery中执行此操作,然后循环遍历它们,如果它们与所选单选按钮匹配,则调整它们的visibility属性。这可能吗?

由于fieldsets中有如此多的代码,我附上下面的屏幕截图以节省空间/使其更具可读性。我试图更改的fieldsets位于RightDiv内。如果您需要更多细节,请告诉我。 THX enter image description here

2 个答案:

答案 0 :(得分: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"}) 

答案 1 :(得分:0)

抱歉,发布时间太早了。试过以下,它工作得很好。

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

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