2010-02-18 131 views
0

我有一个表单,最多可以添加6个子表单。因此,将有6套以下HTML:jQuery在同一超链接上显示/隐藏不同组的表单元素

  <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,然后一切都将默认显示。

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

+0

我不喜欢这类问题。他们看起来像海报只是希望有人为他做他的工作。为什么不看JQuery教程,下载并打印JQuery小抄,然后在遇到困难时询问有关特定问题的问题? – 2010-02-18 13:16:38

+0

LoL!...听起来像功课吗?你想要简单吗?... :) – Reigel 2010-02-18 13:22:01

+0

嗨@乔希。 道歉我同意我有点懒惰。对不起,我会进一步调查,并在以后发生任何问题,当我不可避免地卡住。 – RyanP13 2010-02-18 13:23:27

回答

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(); 
    }); 
}); 

这应该给你你想要的功能。您可能希望通过重置所有输入并选择其默认状态来提高删除功能。

0

您可以放置​​所有带有分隔ID的表格,并使用document.getElementById(“table_id”),style.display =“none”或document.getElementById(“table_id”)。style.display =“块”。

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(); 
    } 
); 

更换livebind,如果你不打算在以后添加链接。