2010-02-18 34 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,如果你不打算在以後添加鏈接。