将onLoad更改为onClick

时间:2014-04-01 16:43:16

标签: javascript forms onclick

所以我正在创建一个撤销日历,并且需要对javascript进行一些更改,而不是确定如何。

我创建的所有代码都必须插入到CMS系统中,该系统会删除head / body标记,因此我无法使用onLoad函数。我想要发生的是,一旦他们选择了日期,他们点击提交按钮,然后出现撤销日期。

以下是我们之前系统中未使用CMS的代码:

Startscript language="javascript" type="text/javascript"<br>
hoyDate = new Date;<br>
closeDate = new Date;<br>
fundDate = new Date;<br>
resDate = new Date;<br>

monthsi = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');<br>
daysi = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');<br>

function setForm() {<br>
document.form.monthy.options.selectedIndex = closeDate.getMonth();<br>
document.form.datey.options.selectedIndex = closeDate.getDate() - 1;<br>
document.form.yeary.value = closeDate.getFullYear();<br>
}<br><br>

function calcRescission() {<br>
closeDate.setMonth(document.form.monthy.options[document.form.monthy.options.selectedIndex].value * 1);<br>
closeDate.setDate(document.form.datey.options[document.form.datey.options.selectedIndex].value * 1);<br>
closeDate.setFullYear(document.form.yeary.value * 1);<br>
<br>
resDate.setTime(closeDate.getTime());<br>
fundDate.setTime(closeDate.getTime());<br>
<br>
for (i=0; i<=2; i++) {<br>
resDate.setTime(resDate.getTime() + (3600000 * 24));<br>
//check for New Year's Day<br>
    if (resDate.getMonth() == 0 && resDate.getDate() == 1) {<br>
    i--;<br>
    }<br>
//check for Christmas<br>
    if (resDate.getMonth() == 11 && resDate.getDate() == 25) {<br>
    i--;<br>
    }<br>
//check for the 4th of July<br>
    if (resDate.getMonth() == 6 && resDate.getDate() == 4) {<br>
    i--;<br>
    }<br>
//check for Veterans Day<br>
    if (resDate.getMonth() == 10 && resDate.getDate() == 11) {<br>
    i--;<br>
    }<br>
//check for Memorial Day<br>
    if (resDate.getMonth() == 4 && resDate.getDate() >= 25 && resDate.getDate() <= 31 && resDate.getDay() == 1) {<br>
    i--;<br>
    }<br>
//check for Labor Day<br>
    if (resDate.getMonth() == 8 && resDate.getDate() >= 1 && resDate.getDate() <= 7 && resDate.getDay() == 1) {<br>
    i--;<br>
    }<br>
//check for Thanksgiving<br>
    if (resDate.getMonth() == 10 && resDate.getDate() >= 22 && resDate.getDate() <= 28 && resDate.getDay() == 4) {<br>
    i--;<br>
    }<br>
//check for Martin Luther King, Jr observance<br>
    if (resDate.getMonth() == 0 && resDate.getDay() == 1 && resDate.getDate() >= 15 && resDate.getDate() <= 21) {<br>
    i--;<br>
    }<br>
//check for Washington/Lincoln observance<br>
    if (resDate.getMonth() == 1 && resDate.getDay() == 1 && resDate.getDate() >= 15 && resDate.getDate() <= 21) {<br>
    i--;<br>
    }<br>
//check for Columbus Day<br>
    if (resDate.getMonth() == 9 && resDate.getDay() == 1 && resDate.getDate() >= 8 && resDate.getDate() <= 14) {<br>
    i--;<br>
    }<br>
//check for Sundays<br>
    if (resDate.getDay() == 0) {<br>
    i--;<br>
    }<br>

}<br>
j = 0;<br>
fundit = "no";<br>
while (j<=3 || fundit != "yes") {<br>
fundit = "yes";<br>
j++;<br>
fundDate.setTime(fundDate.getTime() + (3600000 * 24));<br>
//checking for Christmas<br>
    if ((fundDate.getMonth() == 11 && fundDate.getDate() == 25) || (fundDate.getMonth() == 11 && fundDate.getDate() == 26 && fundDate.getDay() == 1)) {<br>
    j--;<br>
    if (fundDate.getMonth() == 11 && fundDate.getDate() == 25 && fundDate.getDay() == 0) {<br>
    j++;<br>
    }<br>
    fundit = "no";<br>
    }<br>
//checking for New Year's<br>
    if ((fundDate.getMonth() == 0 && fundDate.getDate() == 1) || (fundDate.getMonth() == 0 && fundDate.getDate() == 2 && fundDate.getDay() == 1)) {<br>
    j--;<br>
    if (fundDate.getDate() == 1 && fundDate.getMonth() == 0 && fundDate.getDay() == 0) {
    j++;<br>
    }<br>
    fundit = "no";<br>
    }<br>
//checking for the 4th of July<br>
    if ((fundDate.getMonth() == 6 && fundDate.getDate() == 4) || (fundDate.getMonth() == 6 && fundDate.getDate() == 5 && fundDate.getDay() == 1)) {<br>
    j--;<br>
    if (fundDate.getDate() == 4 && fundDate.getMonth() == 6 && fundDate.getDay() == 0) {
    j++;<br>
    }<br>
    fundit = "no";<br>
    }<br>
//check for Memorial Day<br>
    if (fundDate.getMonth() == 4 && fundDate.getDay() == 1 && fundDate.getDate() >= 25 && fundDate.getDate() <= 31) {<br>
    j--;<br>
    fundit = "no";<br>
    }<br>
//check for Labor day<br>
    if (fundDate.getMonth() == 8 && fundDate.getDay() == 1 && fundDate.getDate() >= 1 && fundDate.getDate() <= 7) {<br>
    j--;<br>
    fundit = "no";<br>
    }<br>
//check for Thanksgiving<br>
    if (fundDate.getMonth() == 10 && fundDate.getDay() == 4 && fundDate.getDate() >= 22 && fundDate.getDate() <= 28) {<br>
    j--;<br>
    fundit = "no";<br>
    }<br>
//check for Veterans Day<br>
    if ((fundDate.getMonth() == 10 && fundDate.getDate() == 11) || (fundDate.getMonth() == 10 && fundDate.getDate() == 12 && fundDate.getDay() == 1)) {<br>
    j--;<br>
    if (fundDate.getDate() == 11 && fundDate.getMonth() == 10 && fundDate.getDay() == 0) {<br>
    j++;<br>
    }<br>
    fundit = "no";<br>
    }<br>
//checking for Martin Luther King, Jr observance<br>
    if (fundDate.getMonth() == 0 && fundDate.getDay() == 1 && fundDate.getDate() >= 15 && fundDate.getDate() <= 21) {<br>
    j--;<br>
    fundit = "no";<br>
    }<br>
//checking for Washington/Lincoln's Day<br>
    if (fundDate.getMonth() == 1 && fundDate.getDay() == 1 && fundDate.getDate() >= 15 && fundDate.getDate() <= 22) {<br>
    j--;<br>
    fundit = "no";<br>
    }<br>
//checking for Columbus Day<br>
    if (fundDate.getMonth() == 9 && fundDate.getDay() == 1 && fundDate.getDate() >= 8 && fundDate.getDate() <= 14) {<br>
    j--;<br>
    fundit = "no";<br>
    }<br>
//checking for weekends<br>
    if (fundDate.getDay() == 6 || fundDate.getDay() == 0)  {<br>
    fundit = "no";<br>
    if (fundDate.getDay() == 0) {<br>
    j--;<br>
    }<br>
    }<br>

}<br>
//feed rescission end date to form<br>
// document.form.rescissionday.value = daysi[resDate.getDay()];<br>

document.form.rescissiondatey.value = resDate.getDate();<br>
document.form.rescissionmonthy.value = monthsi[resDate.getMonth()];<br>
document.form.rescissionyeary.value = resDate.getFullYear();<br>
//feed funding date to form<br>
document.form.fundday.value = daysi[fundDate.getDay()];<br>
document.form.funddatey.value = fundDate.getDate();<br>
document.form.fundmonthy.value = monthsi[fundDate.getMonth()];<br>
document.form.fundyeary.value = fundDate.getFullYear();<br>

document.form.nowday.value = daysi[closeDate.getDay()];<br>
}<br>
Endscript<br>

这是HTML:

<body onload="setForm();calcRescission();">
<form name="form" action="rescission.html" method="post">
    <table cellpadding="4" cellspacing="0">
        <tr>
            <td>
            <select name="monthy" onchange="calcRescission()" class="editable">
                <option value="0">January</option>
                <option value="1">February</option>
                <option value="2">March</option>
            </select>
            <select name="datey" onchange="calcRescission()" class="editable">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>

            </select>
            <input name="yeary" onkeyup="calcRescission()" size="7" class="editable">
            </td>
        </tr>
        <tr>
            <td>
                <input name="rescissionmonthy" readonly size="10" class="rescind">
                <input name="rescissiondatey" readonly size="3" class="rescind">
                <input name="rescissionyeary" readonly size="7" class="rescind">
            </td>
        </tr>
    </table>
</form>

1 个答案:

答案 0 :(得分:1)

你将摆脱这个:

<body onload="setForm();calcRescission();">

并将其添加到脚本中:

 document.getElementsByTagName("body")[0].addEventListener("load", function() {
   setForm();calcRescission()
 }, false);