jQuery在同一个超链接上显示/隐藏不同的表单元素集

时间:2010-02-18 13:10:02

标签: jquery html forms

我有一个表格,最多可添加6个孩子。因此,将有6组以下HTML:

            <table class="portletTable child" cellpadding="0" cellspacing="0" border="0" summary="Please enter some more details regarding your dependants">
            <tr>
                <th>
                    <label for="indTitle">Title</label>
                </th>
                <td colspan="3">
                    <select id="indTitle" class="inlineSpace">
                        <option value="Please select">Please select...</option>
                        <option value="Mr">Mr</option>
                        <option value="Mrs">Mrs</option>
                        <option value="Ms">Ms</option>
                        <option value="Miss">Miss</option>
                        <option value="Dr">Dr</option>
                        <option value="Other">Other</option>
                    </select>
                    <label for="indOther" class="inlineSpace">Other</label>
                    <input type="text" class="text" name="indOther" id="indOther" maxlength="20" />
                </td>
            </tr>
            <tr>
                <th>
                    <label for="firstname">First name</label>
                </th>
                <td colspan="3">
                    <input type="text" class="text" maxlength="50" value="" id="firstname" />
                </td>
            </tr>
            <tr>
                <th>
                    <label for="lastname">Last name</label>
                </th>
                <td colspan="3">
                    <input type="text" class="text" maxlength="50" value="" id="lastname" />
                </td>
            </tr>       
            <tr>
                <th>
                    <label for="dobDay">Date of birth</label>
                </th>
                <td colspan="3">
                    <select name="dobDay" id="dobDay" class="inlineSpace">
                        <option value="day">Day</option>
                    </select>
                    <label for="dobMonth" class="offScreen">Month</label>
                    <select name="dobMonth" id="dobMonth" class="inlineSpace">
                        <option value="month">Month</option>
                    </select>
                    <label for="dobYear" class="offScreen">Month</label>                
                    <select name="dobYear" id="dobYear">
                        <option value="year">Year</option>
                    </select>
                    <p class="fieldNote">You must be over 'minimum partner age' and under 'max partner age'</p>             
                </td>
            </tr>
            <tr>
                <th>
                    Gender
                </th>
                <td colspan="3">
                    <input id="male" name="childGender" class="radio" type="radio" />
                    <label for="male" class="inlineSpace">Male</label>
                    <input id="female" name="childGender" class="radio" type="radio" />
                    <label for="female">Female</label>
                </td>
            </tr>               
        </table>

我需要默认显示第一个孩子,并且要隐藏以下五个孩子。

当用户点击以下链接时,我想要显示第二个孩子,如果他们再次点击它,那么第三个孩子输入show等等......

<tr>
                <th class="dependant">
                    <a href="" class="add">Add another child to your policy</a>
                </th>
            </tr>

显然,当显示第六个孩子时,不应显示链接。

我还需要反过来,因为用户可以选择使用此超链接删除最新添加的子项:

<tr>
                <th class="dependant">
                    <a href="" class="remove">Remove this child from your policy</a>
                </th>
            </tr>

如果关闭Javascript,则默认显示所有内容。

如果您有任何人可以提供帮助,请提前致谢。

3 个答案:

答案 0 :(得分:1)

这应该这样做:

$(function() {
    // Hide everything except the first one
    $('.portletTable').not(':first').hide();

    // Remove functionality
    $('.dependant .remove').click(function() {
        // Hide this child and move it to the end of the table
        $(this)
            .closest('.portletTable')
            .hide()
            .appendTo($(this).closest('form'));
    });

    // Show functionality
    $('.dependant .remove').click(function() {
        // Show the first hidden table
        $(this).closest('form').find('.portletTable:hidden:first').show();
    });
});

这应该可以为您提供所需的功能。您可能希望通过重置所有输入并选择其默认状态来改进删除功能。

答案 1 :(得分:1)

$('.add').live('click',
   function() {
       //seek next element with class "child"
       var $nextChild = $(this).parents('.child').next('.child :first');  
       if ($nextChild.size() > 0)  //exists?
           $nextChild.show();  //show it
   }
);

$('.remove').live('click',
   function() {
       //hide the parent with class "child"
       $(this).parents('.child').hide();
   }
);

如果您不打算稍后添加链接,请将live替换为bind

答案 2 :(得分:0)

您可以将所有表格分开,并使用document.getElementById(“table_id”)。style.display =“none”或document.getElementById(“table_id”)来操纵其可见性状态.style.display =“block ”