简体   繁体   English

我正在尝试简化此代码

[英]I am trying to simplify this code

Can this code be simplified?这段代码可以简化吗? I am pretty sure it can be achieved by abstracting the variable and using an if else or a switch, each case would be the element ID...I'm having aa difficult time working through this.我很确定它可以通过抽象变量并使用 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.

相关问题 我正在尝试将代码简化为嵌套循环 - I'm trying to simplify the code to a nested loop 在尝试用html canvas和javascript绘制塞舌尔标志后尝试使用数组或循环来简化我的代码 - am trying to use arrays or loops to simplify my code after drawing Seychelles flag with html canvas and javascript 我正在尝试使用HTML Javascript制作分数计算器,我需要简化分数该怎么做? - I am trying to make a Fraction Calculator in HTML Javascript, I need to simplify the fraction how can I do this? 尝试使用var而不是数字简化javascript。 我要解决所有这些错误吗? - Trying to simplify javascript with var instead of numbers. Am I going about this all wrong? 我正在尝试将我的多个数组方法调用简化为 if 语句或更简单的基于条件的语句 - I am trying to simplify my multiple array method calls into an if statement or something much more simple based on a condition 我正在尝试结合此javascript代码? - I am trying to combine this javascript code? 我试图理解这段TypeScript代码 - I am trying to understand this piece of TypeScript code 我试图将这些代码分成单独的文件 - I am trying to divide this code in the separate files 我正在尝试了解以下javascript代码 - I am trying to understand the following javascript code JavaScript。 试图简化代码,也许是循环? - JavaScript. Trying to simplify code, maybe with loops?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM