-3

したがって、ASP.NET ページに以下の小さな数字があります。

このページは、私が満足しているグリッドビューで構成されています。各行にチェックボックスがあり、チェックすると行の他のコントロールが有効になり、保存ボタンは行とアクションをデータベースに繰り返します。

私の質問は、以下のコードは私が望むように機能しますが、さらに単純化するための巧妙なトリックはありますか? 私の知識を広げるためにもっと質問がありますか?:)

`<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>

どうもありがとう、オリー

4

1 に答える 1

0

これはテストされていませんが、このように有効および無効にするアイテムに共通のクラスを追加できます

class="rowControl"

次に、次のように調整します。

var myControl = $("[id~="+idstr+"].rowControl"); 
...
if (this.checked) {
   myControl.removeAttr('disabled').removeClass('aspNetDisabled').removeAttr("style");
} else {
   myControl.attr('disabled', 'disabled').addClass('aspNetDisabled').attr('style', newTBStyle);
}

必要なすべてを行うわけではありませんが、そのクラスですべての要素を調整します。不足している部分を追加して戻すことができます (特定の値を設定するなど)。

注:は、含まれるおよび classvar myControl = $("[id~="+idstr+"].rowControl");を持つすべての要素を選択します。ididstrrowControl

于 2013-08-23T13:15:41.777 に答える