【问题标题】:jQuery show/hide different sets of form elements on same hyperlinkjQuery 在同一个超链接上显示/隐藏不同的表单元素集
【发布时间】:2010-02-18 21:21:41
【问题描述】:

我有一个最多可以添加 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>

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

当用户单击以下链接时,我希望显示第二个孩子,如果他们再次单击它,则显示第三个孩子输入,依此类推...

<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,则默认情况下将全部显示。

如果有人能提供帮助,请提前致谢。

【问题讨论】:

  • 我不喜欢这类问题。他们看起来就像海报只是希望有人为他做他的工作。为什么不查看 JQuery 教程,下载并打印 JQuery 备忘单,然后在遇到困难时询问有关特定问题的问题?
  • 大声笑!...听起来像家庭作业?你想要它容易吗?... :)
  • 嗨@乔希。抱歉,我同意我有点懒惰。抱歉,当我不可避免地遇到困难时,我会进一步调查并提出任何问题。

标签: jquery html forms


【解决方案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();
    });
});

这应该会给你你想要的功能。您可能希望通过将所有输入和选择重置为默认状态来改进删除功能。

【讨论】:

    【解决方案2】:
    $('.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

    【讨论】:

      【解决方案3】:

      您可以放置​​具有单独 ID 的所有表格,并使用 document.getElementById("table_id").style.display = "none" 或 document.getElementById("table_id").style.display = "block 操作它们的可见性状态”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-17
        • 1970-01-01
        • 2016-05-17
        • 2017-07-07
        相关资源
        最近更新 更多