繁体   English   中英

我正在尝试简化此代码

[英]I am trying to simplify this code

这段代码可以简化吗? 我很确定它可以通过抽象变量并使用 if else 或 switch 来实现,每种情况都是元素 ID ......我很难解决这个问题。

        function NoTime() {
            if (shortTime == "") {
                timeFormat = "N/A"
            }
        }

        var shortTime;
        var day;
        var month;
        var year;

        var revisionDate = document.getElementById("RevisionDate").value;
        shortTime = revisionDate;
        day = revisionDate.substring(8, 10);
        month = revisionDate.substring(4, 7);
        year = revisionDate.substring(11, 15);
        var timeFormat = day + " " + month + " " + year;
        NoTime();
        $("#RevisionDate")
            .replaceWith("<div id='RevisionDate' class='col-md-12 margin-bottom-10px pad-L-15px border-1px width-174px pad-LR-3px width-227px' /></div>");
        $("#RevisionDate").html(timeFormat);

        var supplierPartInformationEffectiveDate = document.getElementById("SupplierPartInformationEffectiveDate")
            .value;
        shortTime = supplierPartInformationEffectiveDate;
        day = supplierPartInformationEffectiveDate.substring(8, 10);
        month = supplierPartInformationEffectiveDate.substring(4, 7);
        year = supplierPartInformationEffectiveDate.substring(11, 15);
        var timeFormat = day + " " + month + " " + year;
        NoTime();
        $("#SupplierPartInformationEffectiveDate")
            .replaceWith("<div id='SupplierPartInformationEffectiveDate' class='col-md-12 margin-bottom-10px pad-LR-3px border-1px pad-LR-3px width-342px' /></div>");
        $("#SupplierPartInformationEffectiveDate").html(timeFormat);

        var SupplierPartInformationExpirationDate = document.getElementById("SupplierPartInformationExpirationDate")
            .value;
        shortTime = SupplierPartInformationExpirationDate;
        day = SupplierPartInformationExpirationDate.substring(8, 10);
        month = SupplierPartInformationExpirationDate.substring(4, 7);
        year = SupplierPartInformationExpirationDate.substring(11, 15);
        var timeFormat = day + " " + month + " " + year;
        NoTime();
        $("#SupplierPartInformationExpirationDate")
            .replaceWith("<div id='SupplierPartInformationExpirationDate' class='col-md-12 margin-bottom-10px pad-LR-3px border-1px pad-LR-3px width-342px' /></div>");
        $("#SupplierPartInformationExpirationDate").html(timeFormat);
        var idArray = [ RevisionDate, SupplierPartInformationEffectiveDate, SupplierPartInformationExpirationDate ];
        var attrIDArray = ["RevisionDate", "SupplierPartInformationEffectiveDate", "SupplierPartInformationExpirationDate"];
        var stringIDArray = ["#RevisionDate", "#SupplierPartInformationEffectiveDate", "#SupplierPartInformationExpirationDate"];

        for (i = 0; i < idArray.length; i++) {
            var value = document.getElementById(attrIDArray[i]).value;

            var day = value.substring(8, 10);
            var month = value.substring(4, 7);
            var year = value.substring(11, 15);

            var timeFormat = day + " " + month + " " + year;

            if (value == "") {
                timeFormat = "N/A";
            }

            $(stringIDArray[i]).replaceWith("<div id='" + attrIDArray[i] + "' class='col-md-12 margin-bottom-10px pad-L-15px border-1px pad-LR-3px pad-TB-2px darkgrey-border-1px' /></div>");

            if (stringIDArray[i] === "#RevisionDate") {
                $("#RevisionDate").css("width", "227px");
            } else {
                $("#SupplierPartInformationEffectiveDate").css("width", "342px");
                $("#SupplierPartInformationExpirationDate").css("width", "342px");
            }

            $(stringIDArray[i]).html(timeFormat);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM