2013-08-23 61 views
-3

所以我有一個ASP.NET頁面下面的小數字。新增jQuery與ASP.NET,簡化jQuery代碼?

該頁面由一個我很滿意的gridview組成,每一行都有一個複選框,一旦選中,就可以啓用行中的其他控件,一個保存按鈕會遍歷行和數據庫中的動作。

我的問題是,下面的代碼工作如何我想要它,但是有沒有什麼巧妙的技巧進一步簡化?更多的問題擴大我的知識? :)

`<script type="text/javascript"> 
    $(document).ready(function() { 

     //If checkbox in row is checked then 
     $('[id^="MainContent_TCProcurement_TABPurchasing_GVQuotes_CBPurchased1_"]').click(function() { 
      //Checkbox row id 
      var idstr = this.id.replace('MainContent_TCProcurement_TABPurchasing_GVQuotes_CBPurchased1_', ''); 
      //Controls to alter 
      var suppDDL = $("#MainContent_TCProcurement_TABPurchasing_GVQuotes_DDLSuppliers_" + idstr); 
      var qtyPurchased = $("#MainContent_TCProcurement_TABPurchasing_GVQuotes_TBQuantity1_" + idstr); 
      var ratePaid = $("#MainContent_TCProcurement_TABPurchasing_GVQuotes_TBRatePaid1_" + idstr); 
      var buyer = $("#MainContent_TCProcurement_TABPurchasing_GVQuotes_TBBuyer1_" + idstr); 
      var purchasedDate = $("#MainContent_TCProcurement_TABPurchasing_GVQuotes_TBDatePurch1_" + idstr); 
      //If checked then remove disabled and enter some details 
      if (this.checked) { 
       suppDDL.removeAttr('disabled').removeClass('aspNetDisabled').removeAttr("style"); 
       qtyPurchased.removeAttr('disabled').removeClass('aspNetDisabled').removeAttr("style"); 
       ratePaid.removeAttr('disabled').removeClass('aspNetDisabled').removeAttr("style"); 
       buyer.removeAttr('disabled').removeClass('aspNetDisabled').removeAttr("style").val("<%= Session("loggedInUserName")%>"); 
       purchasedDate.removeAttr('disabled').removeClass('aspNetDisabled').removeAttr("style").val("<%= Date.Now()%>"); 
      } else { 
       var newTBStyle = "font-family: Arial; font-size: 1em; background-color: rgb(235, 235, 228);"; 
       suppDDL.attr('disabled', 'disabled').addClass('aspNetDisabled').attr('style', newTBStyle); 
       qtyPurchased.attr('disabled', 'disabled').addClass('aspNetDisabled').attr('style', newTBStyle); 
       ratePaid.attr('disabled', 'disabled').addClass('aspNetDisabled').attr('style', newTBStyle); 
       buyer.attr('disabled', 'disabled').addClass('aspNetDisabled').attr('style', newTBStyle).val(""); 
       purchasedDate.attr('disabled', 'disabled').addClass('aspNetDisabled').attr('style', newTBStyle).val(""); 
      } 
     }); 

    }); 
</script> 

非常感謝, 奧利

+1

哎呀那些asp.net id的名字傷害了我的眼睛。你不能使用簡單的ID而不是舊式的自動生成的ID?也許你不能,但檢查了這一點:http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client -ids-vs-2010-and-net-4-0-series.aspx不僅在眼睛上更容易,而且可能更少出錯。 – Robert

+0

不一定簡單,但我會將您的'