2012-07-20 62 views
0

我有一個複選框的onChange事件,該複選框觸發一個切換某些TR的可見性的函數。我的問題是這個代碼只適用於Firefox。我希望它爲ie8工作。ASP.NET jquery檢查檢查不起作用

function toggleVisibility() { 

    if ($("#ctl00_PageContent_chkVisibility").is(':checked')) { 
     $('#ctl00_PageContent_freight_rate_column_chaair tr').each(function(i, val) { // Loop through rows in grid 
      if (i > 9 & i < 28) { 
       $('#ctl00_PageContent_freight_rate_column_chaair_r' + i).hide(); 
      }; 
     }); 
    } 
    else { 
     $('#ctl00_PageContent_freight_rate_column_chaair tr').each(function(i, val) { // Loop through rows in grid 
      if (i > 9 & i < 28) { 
       $('#ctl00_PageContent_freight_rate_column_chaair_r' + i).show(); 
      }; 
     }); 
    }; 
}; 

有沒有人知道這樣做的更好方法?

感謝

回答

1

我會嘗試使用「開始與」選擇,而不是試圖Concat的ID以索引號。嘗試是這樣的:

$('#ctl00_PageContent_chkVisibility').change(function() { 
    toggleVisibility() 
}); 

function toggleVisibility() { 
    if ($("#ctl00_PageContent_chkVisibility").is(':checked')) { 
     $('#ctl00_PageContent_freight_rate_column_chaair tr').each(function (i, val) { // Loop through rows in grid 
      if (i > 9 & i < 28) { 
       $('input[id^="ctl00_PageContent_freight_rate_column_chaair_r"]').hide(); 
      }; 
     }); 
    } 
    else { 
     $('#ctl00_PageContent_freight_rate_column_chaair tr').each(function (i, val) { // Loop through rows in grid 
      if (i > 9 & i < 28) { 
       $('input[id^="ctl00_PageContent_freight_rate_column_chaair_r"]').show(); 
      }; 
     }); 
    }; 
}; 

您應該不需要,因爲在循環過程中Concat的ID以索引號,你只需要檢查的指標。在該索引處,隱藏功能將隱藏所述索引處的行,因此不需要指定想要隱藏的行的確切ID。

+0

嘿感謝幫我。在我的if條件中,我使用了我的原始jquery選擇器,因爲它隱藏了我的用戶控件,而不是根據ID隱藏正確的行。謝謝。 – 2012-07-20 14:05:43

+0

沒問題,我很高興能幫上忙! – ORION 2012-07-20 14:09:28

0

我想你已經聲明這樣的事情你的aspx頁面上:

<asp:CheckBox ID="chkId" runat="server" onchange="toggleVisibility()"/> 

這部作品FF但不是在IE瀏覽器。

的複選框元素卸下onchange屬性,並刪除功能toggleVisibility 並嘗試使用jquery的註冊你的函數:

<script language="javascript"> 
$(function() { 

$('#ctl00_PageContent_chkVisibility').change(function() {    
     // 
     // paste here your existing code , the body of the function toggleVisibility 
}); 
}); 
</script>