JS计算器在wordpress中不起作用,脚本在旧站点上起作用

时间:2018-09-27 15:57:19

标签: javascript wordpress

这个问题本周有问题。我们正在创建一个新站点,因为当前站点有些过时了。当前站点上有一系列运行良好的财务计算器。

在将脚本和内容传输到新的wordpress网站时,我们收到一条错误消息,使它无法正常工作。

我们得到的错误是:

    TypeError: document.formc is undefined[Learn More] accounting-calc.js:7:3
recalc_onclick
http://77.104.171.166/~paramo48/grtglobal.com/wp-content/themes/child-theme/js/accounting-calc.js:7:3
onclick
http://77.104.171.166/~paramo48/grtglobal.com/business-financial-calculators/:1:1

我们使用的脚本是:

    /* <![CDATA[ */
var co = new Object;

function recalc_onclick(ctl) {
    if (ctl == '') {
        co.pA1B = eeparseFloatTh(document.formc.pA1B.value);
        co.pA2B = eeparsePercent(document.formc.pA2B.value);
        co.pA3B = eeparseFloat(document.formc.pA3B.value);
        calc(co);
        document.formc.pA4B.value = eedatefmt(fmtdate5, co.pA4B);
        document.formc.pA5B.value = eedatefmt(fmtdate5, co.pA5B);
    };
};
var eeisus = 0;
var eetrue = "TRUE";
var eefalse = "FALSE";
var eedec = ".";
var eeth = ",";
var eedecreg = new RegExp("[.]", "g");
var eethreg = new RegExp(",", "g");
var fmtdaynamesshort = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var fmtdaynameslong = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var fmtmonthnamesshort = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var fmtmonthnameslong = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var fmtstrings = new Array(",", " ", "");
var fmtdate5 = new Array(34, 25, 2);
var fmtdate6 = new Array(34, 25, 0);

function calc(data) {
    var cA1B = data.pA1B;
    var cA2B = data.pA2B;
    var cA3B = data.pA3B;
    var cA4B = (((pmt((((cA2B) / (12))), (((cA3B) * (12))), (cA1B), (0), (0))) * (-1)));
    var cA5B = (((((cA1B) * (cA2B))) / (12)));
    data.pA4B = cA4B;
    data.pA5B = cA5B;
};

function myIsNaN(x) {
    return (isNaN(x) || (typeof x == 'number' && !isFinite(x)));
};

function mod(n, d) {
    return n - d * Math.floor(n / d);
};

function round(n, nd) {
    if (isFinite(n) && isFinite(nd)) {
        var sign_n = (n < 0) ? -1 : 1;
        var abs_n = Math.abs(n);
        var factor = Math.pow(10, nd);
        return sign_n * Math.round(abs_n * factor) / factor;
    } else {
        return NaN;
    }
};

function eeparseFloat(str) {
    str = String(str).replace(eedecreg, ".");
    var res = parseFloat(str);
    if (isNaN(res)) {
        return 0;
    } else {
        return res;
    }
};

function eeparsePercent(str) {
    var parts = String(str).split('%');
    var tmp = String(parts[0]).replace(eedecreg, ".");
    var res = parseFloat(tmp) / 100;
    if (isNaN(res)) {
        return 0;
    } else {
        return res;
    }
};

function eedisplayFloat(x) {
    if (myIsNaN(x)) {
        return Number.NaN;
    } else {
        return String(x).replace(/\./g, eedec);
    }
};

function eedisplayScientific(x, nd) {
    if (myIsNaN(x)) {
        return Number.NaN;
    } else {
        var str = String(x.toExponential(nd));
        return str.replace(/\./g, eedec);
    }
};

function eedisplayFloatND(x, nd) {
    if (myIsNaN(x)) {
        return Number.NaN;
    } else {
        var res = round(x, nd);
        if (nd > 0) {
            var str = String(res);
            if (str.indexOf('e') != -1) return str;
            if (str.indexOf('E') != -1) return str;
            var parts = str.split('.');
            if (parts.length < 2) {
                var decimals = ('00000000000000').substring(0, nd);
                return (parts[0]).toString() + eedec + decimals;
            } else {
                var decimals = ((parts[1]).toString() + '00000000000000').substring(0, nd);
                return (parts[0]).toString() + eedec + decimals;
            }
        } else {
            return res;
        }
    }
};

function eedisplayPercent(x) {
    if (myIsNaN(x)) {
        return Number.NaN;
    } else {
        var tmp = (x * 100).toString() + '%';
        return tmp.replace(/\./g, eedec);
    }
};

function eedisplayPercentND(x, nd) {
    if (myIsNaN(x)) {
        return Number.NaN;
    } else {
        return eedisplayFloatND(x * 100, nd) + '%';
    }
}

function eeparseFloatTh(str) {
    str = String(str).replace(eethreg, "");
    str = String(str).replace(eedecreg, ".");
    var res = parseFloat(str);
    if (isNaN(res)) {
        return 0;
    } else {
        return res;
    }
};

function eedisplayFloatNDTh(x, nd) {
    if (myIsNaN(x)) {
        return Number.NaN;
    } else {
        var res = round(x, nd);
        if (nd > 0) {
            var str = String(res);
            if (str.indexOf('e') != -1) return str;
            if (str.indexOf('E') != -1) return str;
            var parts = str.split('.');
            var res2 = eeinsertThousand(parts[0].toString());
            if (parts.length < 2) {
                var decimals = ('00000000000000').substring(0, nd);
                return (res2 + eedec + decimals);
            } else {
                var decimals = ((parts[1]).toString() + '00000000000000').substring(0, nd);
                return (res2 + eedec + decimals);
            }
        } else {
            return (eeinsertThousand(res.toString()));
        }
    }
};

function eedisplayPercentNDTh(x, nd) {
    if (myIsNaN(x)) {
        return Number.NaN;
    } else {
        return eedisplayFloatNDTh(x * 100, nd) + '%';
    }
}

function eeinsertThousand(whole) {
    if (whole == "" || whole.indexOf("e") >= 0) {
        return whole;
    } else {
        var minus_sign = "";
        if (whole.charAt(0) == "-") {
            minus_sign = "-";
            whole = whole.substring(1);
        };
        var res = "";
        var str_length = whole.length - 1;
        for (var ii = 0; ii <= str_length; ii++) {
            if (ii > 0 && ii % 3 == 0) {
                res = eeth + res;
            };
            res = whole.charAt(str_length - ii) + res;
        };
        return minus_sign + res;
    }
};

function eedatefmt(fmt, x) {
    if (!isFinite(x)) return Number.NaN;
    var tmp = 0;
    var res = "";
    var len = fmt.length;
    for (var ii = 0; ii < len; ii++) {
        if (fmt[ii] > 31) {
            res += fmtstrings[fmt[ii] - 32];
        } else {
            switch (fmt[ii]) {
                case 2:
                    res += eemonth(x);
                    break;
                case 3:
                    tmp = eemonth(x);
                    if (tmp < 10) {
                        res += "0";
                    };
                    res += tmp;
                    break;
                case 4:
                    res += fmtmonthnamesshort[eemonth(x) - 1];
                    break;
                case 5:
                    res += fmtmonthnameslong[eemonth(x) - 1];
                    break;
                case 6:
                    res += eeday(x);
                    break;
                case 7:
                    tmp = eeday(x);
                    if (tmp < 10) {
                        res += "0";
                    };
                    res += tmp;
                    break;
                case 8:
                    res += fmtdaynamesshort[weekday(x, 1) - 1];
                    break;
                case 9:
                    res += fmtdaynameslong[weekday(x, 1) - 1];
                    break;
                case 10:
                    tmp = year(x) % 100;
                    if (tmp < 10) {
                        res += "0";
                    };
                    res += tmp;
                    break;
                case 11:
                    res += year(x);
                    break;
                case 12:
                    res += hour(x);
                    break;
                case 13:
                    tmp = hour(x);
                    if (tmp < 10) {
                        res += "0";
                    };
                    res += tmp;
                    break;
                case 14:
                    tmp = hour(x) % 12;
                    if (tmp == 0) {
                        res += "12";
                    } else {
                        res += tmp % 12;
                    };
                    break;
                case 15:
                    tmp = hour(x) % 12;
                    if (tmp == 0) {
                        res += "12";
                    } else {
                        if (tmp < 10) {
                            res += "0";
                        };
                        res += tmp;
                    };
                    break;
                case 16:
                    res += minute(x);
                    break;
                case 17:
                    tmp = minute(x);
                    if (tmp < 10) {
                        res += "0";
                    };
                    res += tmp;
                    break;
                case 18:
                    res += second(x);
                    break;
                case 19:
                    tmp = second(x);
                    if (tmp < 10) {
                        res += "0";
                    };
                    res += tmp;
                    break;
                case 21:
                case 22:
                    if (hour(x) < 12) {
                        res += "AM";
                    } else {
                        res += "PM";
                    };
                    break;
                case 23:
                    res += eedisplayFloat(x);
                    break;
                case 24:
                    tmp = fmt[++ii];
                    res += eedisplayFloatND(x, tmp);
                    break;
                case 25:
                    tmp = fmt[++ii];
                    res += eedisplayFloatNDTh(x, tmp);
                    break;
                case 26:
                    res += eedisplayPercent(x);
                    break;
                case 27:
                    tmp = fmt[++ii];
                    res += eedisplayPercentND(x, tmp);
                    break;
                case 28:
                    tmp = fmt[++ii];
                    res += eedisplayPercentNDTh(x, tmp);
                    break;
                case 29:
                    tmp = fmt[++ii];
                    res += eedisplayScientific(x, tmp);
                    break;
            };
        };
    };
    return res;
};

function leap_gregorian(year) {
    return ((year % 4) == 0) && (!(((year % 100) == 0) && ((year % 400) != 0)));
}
var GREGORIAN_EPOCH = 1721425;

function gregorian_to_jd(year, month, day) {
    return (GREGORIAN_EPOCH - 0) + (365 * (year - 1)) + Math.floor((year - 1) / 4) + (-Math.floor((year - 1) / 100)) + Math.floor((year - 1) / 400) + Math.floor((((367 * month) - 362) / 12) + ((month <= 2) ? 0 : (leap_gregorian(year) ? -1 : -2)) + day);
}

function jd_to_gregorian(jd) {
    var wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad, yindex, year, yearday, leapadj;
    wjd = Math.floor(jd);
    depoch = wjd - GREGORIAN_EPOCH - 1;
    quadricent = Math.floor(depoch / 146097);
    dqc = mod(depoch, 146097);
    cent = Math.floor(dqc / 36524);
    dcent = mod(dqc, 36524);
    quad = Math.floor(dcent / 1461);
    dquad = mod(dcent, 1461);
    yindex = Math.floor(dquad / 365);
    year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex;
    if (!((cent == 4) || (yindex == 4))) {
        year++;
    }
    yearday = wjd - gregorian_to_jd(year, 1, 1);
    leapadj = ((wjd < gregorian_to_jd(year, 3, 1)) ? 0 : (leap_gregorian(year) ? 1 : 2));
    var month = Math.floor((((yearday + leapadj) * 12) + 373) / 367);
    var day = (wjd - gregorian_to_jd(year, month, 1)) + 1;
    return new Array(year, month, day);
}

function eeday(serial_number) {
    if (!isFinite(serial_number)) return Number.NaN;
    if (serial_number < 1) {
        return 0;
    }
    if (serial_number > 60) serial_number--;
    var res = jd_to_gregorian(serial_number + 2415020);
    return res[2];
};

function hour(serial_number) {
    if (!isFinite(serial_number)) return Number.NaN;
    var res = Math.floor((serial_number - Math.floor(serial_number)) * 86400 + 0.5);
    return Math.floor(res / 3600);
}

function minute(serial_number) {
    if (!isFinite(serial_number)) return Number.NaN;
    var res = Math.floor((serial_number - Math.floor(serial_number)) * 86400 + 0.5);
    return Math.floor(res / 60) % 60;
};

function eemonth(serial_number) {
    if (!isFinite(serial_number)) return Number.NaN;
    if (serial_number < 1) {
        return 1;
    }
    if (serial_number > 60) serial_number--;
    var res = jd_to_gregorian(serial_number + 2415020);
    return res[1];
};

function second(serial_number) {
    if (!isFinite(serial_number)) return Number.NaN;
    var res = Math.floor((serial_number - Math.floor(serial_number)) * 86400 + 0.5);
    return res % 60;
};

function weekday(serial_number, return_type) {
    if (!isFinite(return_type) || !isFinite(serial_number)) return Number.NaN;
    if (return_type < 1 || return_type > 3) return Number.NaN;
    var res = Math.floor(serial_number + 6) % 7;
    switch (Math.floor(return_type)) {
        case 1:
            return res + 1;
        case 2:
            return (res + 6) % 7 + 1;
        case 3:
            return (res + 6) % 7;
    };
    return "hej";
};

function year(serial_number) {
    if (!isFinite(serial_number)) return Number.NaN;
    if (serial_number < 1) {
        return 1900;
    }
    if (serial_number > 60) serial_number--;
    var res = jd_to_gregorian(serial_number + 2415020);
    return res[0];
};

function pmt(rate, nper, pv, fv, type) {
    if (rate == 0) {
        return -pv / nper;
    } else {
        var pvif = Math.pow(1 + rate, nper);
        var fvifa = (Math.pow(1 + rate, nper) - 1) / rate;
        var type1 = (type != 0) ? 1 : 0;
        return ((-pv * pvif - fv) / ((1 + rate * type1) * fvifa));
    }
};
/* ]]> */

,内容为:

    <form id="formc" method="post" action="" class="calc normal">
<noscript><p>Your browser does not support Javascript or has Javascript disabled.<br />Our calculators will not work without it.</p><p>If you want to enable Javascript but are unsure of how to do it, visit our <a href="/site_help/index.php">website help page</a></p></noscript>
<p>See how much you can afford to borrow...</p>

<fieldset><legend><strong>Calculator</strong></legend>
<p><label for="pA1B">Amount of loan</label><span>£</span><input name="pA1B" id="pA1B" onblur="this.value=eedisplayFloatNDTh(eeparseFloatTh(this.value),0);recalc_onclick('pA1B')" value="" type="text"></p>
<p><label for="pA2B">Interest rate</label><span>%</span><input name="pA2B" id="pA2B" onblur="this.value=eedisplayPercentND(eeparsePercent(this.value),2);recalc_onclick('pA2B')" value="" type="text"></p>
<p><label for="pA3B">Term in years</label><span> </span><input name="pA3B" id="pA3B" onblur="this.value=eedisplayFloat(eeparseFloat(this.value));recalc_onclick('pA3B')" value="" type="text"></p>
<p class="formresult"><label for="pA4B">Monthly payment</label><span>£</span><input name="pA4B" tabindex="-1" id="pA4B" value="Result" readonly="readonly" type="text"></p>
<p class="formresult"><label for="pA5B">Interest only</label><span>£</span><input name="pA5B" tabindex="-1" id="pA5B" value="Result" readonly="readonly" type="text"></p>
<p><button type="button" name="xl_update_bottom" onclick="recalc_onclick('')">Calculate</button><button type="button" name="xl_reset_bottom" onclick="reset_onclick('')">Reset</button></p>
</fieldset>
<script>
/* <![CDATA[ */
function reset_onclick(x){document.formc.reset();postcode();recalc_onclick('');};function postcode(){};function eequerystring(){var querystring=document.location.search;if(querystring.length>0){variables=(querystring.substring(1)).split("&");var variable;var key;var value;for(var ii=0;ii<variables.length;ii++){variable=variables[ii].split("=");key=unescape(variable[0]);value=unescape(variable[1]);if(document.formc[key]!=null){document.formc[key].value=value;}}}}function initial_update(){postcode('');eequerystring();recalc_onclick('');}
/* ]]> */</script>
</form>

我敢肯定这很简单,但是我还没有解决。

起初我以为formc是一个错字,但在它可以工作的原始站点上是完全一样的。

1 个答案:

答案 0 :(得分:1)

您缺少表单上的name属性。

console.log(document.formc)
<form id="formc" name="formc">
</form>