2009-12-20 17 views
0

我對服務器(C#)一個循環,這樣處理:使用jQuery的表格操作 - 行數是關閉

for(i=0;i<=Request.Files.Count - 1; i++) 
{ 
    // tell the client that the upload is about to happen 
    // and report useful information 
    Update(percent, message, i, 0); 

    System.Threading.Thread.Sleep(1000); 

    // tell the client that upload succeeded 
    // and report useful information 
    Update(percent, message, i, 1); 

} 

「更新」寫入到客戶端的JavaScript函數「PublishUpdate」功能。 行參數是包含上傳文件的表中的行。 '狀態'告訴我們文件即將被上傳(0)還是完成(1)。

問題是,我似乎無法得到正確的計數。循環似乎在 開始2或3行放入表中或(在播放行值後)它在 最後一行之前結束。我對jQuery非常陌生。有什麼東西看起來顯然是錯的嗎?

function PublishUpdate(percent, message, row, status) 
    { 
     var bodyRows = $("#picTableDisplay tbody tr"); 

      bodyRows.each(function(index){ 
       if (index == row && status == 0)     
       $('#status'+index).html("<img alt='inproc' src='images/loading.gif' />"); 
       else if (index == row && status == 1) 
       $('#status'+index).html("complete"); 

    }); 

} 

最後,表看起來是這樣的:提前

<table width="100%" cellspacing="0" cellpadding="3" border="0" align="center" id="picTableDisplay" summary="" class="x-pinfo-table"> 
         <tbody id="files_list" class="scrollContent"> 
          <tr class="evenRow"> 
           <td class="numCol" id="num0"> 
           </td> 
           <td class="fnameCol" id="fname0"> 
           </td> 
           <td class="statusCol" nowrap="" id="status0"> 
           </td> 
           <td class="remCol" id="rem0"> 
           </td> 
          </tr> 
          <tr class="oddRow"> 
           <td class="numCol" id="num1"> 
           </td> 
           <td class="fnameCol" id="fname1"> 
           </td> 
           <td class="statusCol" nowrap="" id="status1"> 
           </td> 
           <td class="remCol" id="rem1"> 
           </td> 
          </tr> 
          <tr class="evenRow"> 
           <td class="numCol" id="num2"> 
           </td> 
           <td class="fnameCol" id="fname2"> 
           </td> 
           <td class="statusCol" nowrap="" id="status2"> 
           </td> 
           <td class="remCol" id="rem2"> 
           </td> 
          </tr> 
          AND SO ON ... 

感謝。

+0

爲什麼有')'後,關閉大括號? – kiamlaluno 2009-12-20 23:06:16

+0

要完成.each( – Rich 2009-12-20 23:07:42

+0

但最後}缺失! – Rich 2009-12-20 23:08:13

回答

0

C#使用零索引,並且通常HTML作者使用索引從一開始。檢查,看看是否需要更正從0到索引1爲主,像這樣:

$('#status' + (index + 1)) 

而且重構你的代碼更簡單的東西往往可以修復隱藏的錯誤,或者至少使錯誤更加明顯。我建議沿着這些路線的東西:

if (index == row) 
{ 
    if (status == 0) { 
     html = "<img alt='inproc' src='images/loading.gif' />"; 
    } else { 
     html = "complete"; 
    } 
    $('#status'+index).html(html); 
} 

你也應該使用C#成語循環,< x<= x - 1

for(i=0; i < Request.Files.Count; i++) 
+0

jQuery的「每個」方法開始索引在0 – Rich 2009-12-20 23:35:09

+0

是的,他正在尋找'#status0',但我敢打賭,第一個元素在HTML中被稱爲「#status1」 – 2009-12-20 23:42:16

+0

感謝您的幫助,這是一個很好的猜測,但表格ID標籤也從0開始(我將表格添加到了我的文章中) – 2009-12-21 03:59:14

0

我不完全相信你正在試圖做的是什麼目前還不清楚#status元素的位置。但是,假設他們是行內的細胞中,這可能會更好,給他們一個班「狀態」,然後寫類似

function PublishUpdate(percent, message, row, status) { 
    $('#picTableDisplay tbody tr:eq('+row+') .status').html(
     status==0 ? '<img alt="inproc" src="images/loading.gif"/>' : 'complete' 
    ); 
}