2010-05-26 101 views
1

我有一個簡單的表格來模擬甘特圖。這個想法是,每個TD點擊觸發一個隱藏的輸入文本(值的數組)中存儲的值的總和,在TH(TR標籤的第一個子)中可用。這些值的行總和顯示在最終的TD上。TD上的jQuery求和事件點擊

接下來,代碼表:

<table width="100%" cellspacing="0" class="datagrid_ppal"> 
    <tbody> 
     <tr class="encabezado"> 
     <th rowspan="2" scope="col" width="15%">Area</th> 
     <th colspan="7" scope="col">Month</th> 
     </tr> 
     <tr class="encabezado"> 
     <th scope="col" width="2%">1</th> 
     <th scope="col" width="2%">2</th> 
     <th scope="col" width="2%">3</th> 
     <th scope="col" width="2%">4</th> 
     <th scope="col" width="2%">5</th> 
     <th scope="col" width="2%">...</th> 
     <th scope="col" width="2%">31</th> 
     </tr> 
     <tr> 
     <th scope="row">Area 1<input name="line_config" type="hidden" value="0,5,50" /></th> 
     <td class="gantt"> </td> 
     <td class="gantt"> </td> 
     <td class="gantt"> </td> 
     <td class="gantt"> </td> 
     <td class="gantt"> </td> 
     <td class="gantt">...</td> 
     <td class="gantt"> </td> 
     </tr> 
     <tr> 
     <th scope="row">Area 2 <input name="line_config" type="hidden" value="0,0,10" /></th> 
     <td class="gantt"> </td> 
     <td class="gantt"> </td> 
     <td class="gantt"> </td> 
     <td class="gantt"> </td> 
     <td class="gantt"> </td> 
     <td class="gantt">...</td> 
     <td class="gantt"> </td> 
     </tr> 
     <tr class="encabezado"> 
     <th scope="row">Total</th> 
     <td class="total_dia"> </td> 
     <td class="total_dia"> </td> 
     <td class="total_dia"> </td> 
     <td class="total_dia"> </td> 
     <td class="total_dia"> </td> 
     <td class="total_dia">...</td> 
     <td class="total_dia"> </td> 
     </tr> 
    </tbody> 
    </table> 

值的陣列的工作原理如下: 數組[0]:關; array [1]:術前; 數組[2]:操作。

這是我用來區分預操作和可操作單元的jQuery代碼。如果某個單元處於非活動狀態,則該區域關閉:

$(document).ready(function() { 
    $('td.gantt').click(function() { 
     $(this).toggleClass('preop'); 
    }); 
    $('td.gantt').dblclick(function() { 
     $(this).toggleClass('operating'); 
    }); 
}); 

非活動TD總是等於總和。我想做的事?簡單: 1.如果TD沒有被點擊(無效),那jQuery總是總結TH輸入值[0]。 2.當我單擊或雙擊時,TD.gantt jQuery獲取TH輸入值[1],並將其添加到總數中。

如果任何人都可以幫助解決整個問題,我會很感激。

回答

1

混合「click」和「dblclick」不起作用,至少不可靠。我建議你重新進行交互,以便「點擊」在三種不同的狀態中循環。

做你想做的事(我不完全理解)數學的基本問題是讓<th>內容對應於每個點擊<td>是在列。爲了得到這一點,你」我們必須找到<td>屬於其父<tr>的那個孩子,然後可以找到<th>(帶有「:nth-​​child」選擇器或「eq」過濾器)。

+0

謝謝。我已經找到了通過「$(this).parent()。children('input:hidden');」來搜索​​父級,然後的方法。現在的問題是動態從隱藏的輸入數組中獲取值:array [0]:Off;陣列[1]:術前;數組[2]:操作。 – betacar 2010-06-01 14:39:36

+0

Answer found:http://stackoverflow.com/questions/2950631/jquery-getting-value-from-input-array/2950663#2950663 =) – betacar 2010-06-01 19:32:42