在JavaScript中使用循环无法显示年份

时间:2014-03-29 05:01:46

标签: javascript html

enter image description here

完整的HTML代码(head标记中的脚本是图像旋转器):

<html>

<head>
    <title>Credit card details</title>
        <link href="home.css" rel="stylesheet" type="text/css"/>
        <link href="booking.css" rel="stylesheet" type="text/css"/>
        <script src="val_registration.js" type="text/javascript"></script>
    <script language="JavaScript1.2">
    var howOften = 5; //number often in seconds to rotate
    var current = 0; //start the counter at 0
    var ns6 = document.getElementById&&!document.all; //detect netscape 6

    // place your images, text, etc in the array elements here
    var items = new Array();
    items[0]="<a href='link.htm'><img alt='photo01' src='photo01.jpg' height='237' width='750' border-style='inset' border-weight:'10px' /></a>"; //a linked image
    items[1]="<a href='link.htm'><img alt='photo02' src='photo02.jpg' height='237' width='750' border-style='inset'  border-weight:'10px'/></a>"; //a linked image
    items[2]="<a href='link.htm'><img alt='photo03' src='photo03.jpg' height='237' width='750' border-style='inset'  border-weight:'10px'/></a>"; //a linked image
    items[3]="<a href='link.htm'><img alt='photo04' src='photo04.jpg' height='237' width='750' border-style='inset'  border-weight:'10px'/></a>"; //a linked image
    items[4]="<a href='link.htm'><img alt='photo05' src='photo05.jpg' height='237' width='750' border-style='inset'  border-weight:'10px'/></a>"; //a linked image

    function rotater() {
        document.getElementById("placeholder").innerHTML = items[current];
        current = (current==items.length-1) ? 0 : current + 1;
        setTimeout("rotater()",howOften*1000);
    }

    function rotater() {
        if(document.layers) {
            document.placeholderlayer.document.write(items[current]);
            document.placeholderlayer.document.close();
        }
        if(ns6)document.getElementById("placeholderdiv").innerHTML=items[current]
            if(document.all)
                placeholderdiv.innerHTML=items[current];
                current = (current==items.length-1) ? 0 : current + 1; //increment or reset
                setTimeout("rotater()",howOften*1000);
    }
    window.onload=rotater;
    </script>
</head>

<body>
    <div id="login" name="login">
        &nbsp <a href="login.htm" target="_self"> Login</a>&nbsp&nbsp<b><b>|</b></b>
        &nbsp<a href="signup.htm" target="_self">New user?</a>&nbsp&nbsp
    </div>

    <img src="logo.jpg" alt="logo" id="logo" width="500" height="100" usemap="#logomap"/></br>
    <map id="logomap" name="logomap">
        <area shape="rect" coords="0,0,743,146" href="home.htm" alt="home"/>
        <area shape="default" coords"0,0,743,146" href="home.htm" alt="home"/>
    </map></br>

    <div id="placeholderdiv"></div><br/>

    <div id="mlink" >
        <a href="home.htm" target="_self" id="home"> Home </a>
        <a href="aboutus.htm" target="_self" id="aboutus"> About Us </a>
        <a href="promotion.htm" target="_self" id="promotion"> Promotion </a>
        <a href="contactus.htm" target="_self" id="contactus"> Contact Us </a>
        <a href="faqs.htm" target="_self" id="faqs"> FAQs </a>  
    </div><br/>

    <div id="opac">
        <h1> Credit card details </h1>
    <hr/>
        <form id="cc_form" action="success(payment).html" method="post" onsubmit="return val_cc()">
        <table rules="none" cellpadding="10px" cellspacing="10px">
            <tr>
                <td><label for="cc_no">Credit card number:<span id="imp">*</span></label></td><td><input type="text" id="cc_no" tabindex="1"/>
                <br/><span class="eg">&nbsp;eg:5678123445468909</span></td>
            </tr>

            <tr>
                <td><label for="cvv">Card Verification Value(Last 3 digits<br/> of credit card number):<span id="imp">*</span></label></td><td>
                <input type="text" id="cvv" tabindex="2"/><br/><span class="eg">&nbsp;eg:909</span></td>
            </tr>

            <tr>
                <td><label for="expiry_day">Expiry date(MM/YYYY):<span id="imp">*</span></label></td>
                <td>
                    <select id="expiry_month" tabindex="4">
                        <optgroup label="Month">
                        <option value="01">January</option>
                        <option value="02">February</option>
                        <option value="03">March</option>
                        <option value="04">April</option>
                        <option value="05">May</option>
                        <option value="06">June</option>
                        <option value="07">July</option>
                        <option value="08">August</option>
                        <option value="09">September</option>
                        <option value="10">October</option>
                        <option value="11">November</option>
                        <option value="12">December</option>
                        </optgroup>
                    </select>

                    <select id="expiry_year" tabindex="5">
                        <optgroup>
                        <script>generate_year();</script>   
                        </optgroup>
                    </select>
                </td>
            </tr>

            <tr>
                <td><label for="cardholder">Cardholder's name(Name as displayed<br/> on the card):<span id="imp">*</span></label></td>
                <td><input type="text" id="cardholder" tabindex="6"/><br/><span class="eg">&nbsp;eg:Yap Kar Kei</span></td>
            </tr>

            <tr>
                <td><input type="submit" value="Confirm" id="confirm2" tabindex="7"/>
                <input type="reset" value="Cancel" id="cancel2" tabindex="8"/></td>
            </tr>

            <tr>
                <td>Notes: <span id="imp">*</span> indicates the column that must be filled</td>
            </tr>
        </table>
        </form>
    </div>

    <hr/>
    <b><i id="copyright">Copyrighted : &#169;  2014  I &hearts; Travels agency. </i></b>
</body>

JavaScript代码:

function generate_year()            /*For generate cc year*/

{
    for (var i = 2014; i <= 2104; i++)

    {
        document.write ("<option value='" + i + "'>" + i + "</option>");

    }

}

实际上我想以这种格式显示所有选项:2014。 但是,我不知道我的代码有什么问题,直到多年没有出现。 (1月旁边)

1 个答案:

答案 0 :(得分:1)

您的信息页上是否有 generate_year 功能? 我在完整的html代码中的head script部分中没有看到此函数。 您的函数 generate_year 绝对正确且有效。