表单标签,常用javascript,日历

本文介绍了一个自定义的日历控件实现方法,包括如何通过JavaScript和HTML创建交互式的月历界面,支持用户选择日期并返回选定日期的功能。文章详细解释了设置年份和月份下拉列表、显示日历网格及日期导航等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<select>
<option value="" selected></option>
.....
</select>
<textarea name,cols,rows></textarea>

<input name="" type="" maxlength="" size=""(宽度) value="">
有文本框,密码框,隐藏,单选,多选框.
<input type="text" password,hidden,sumbit
           checkbox checked
获取值的方法都为
request.getParmeter("name");

 


收集下拉框属性.select ,option

 

文本无边框,当标签使用 ,表格的显示与隐藏
<input name,type="text" disbled/readonly,class="noborder"  value="要显示的文字"
    size="15"  onclick=table1.style.display=block/none>;

写js的地方有两处
1 <script></script>之间
2 标签中<body onload="javascript:window.setTimeOut('window.close(),5000')">
或省略<body onload="window.setTimeOut('window.close(),5000')">

window对像的常用方法
alert.....
window.open();一般弹窗,showModalDialog();网页对话框
setTimeOut();


父窗口
window.opener
window.opener.location.reload();刷新

 

 

 日历

要使用日历控件的地方

<INPUT class=input2 id=overtime onClick="loadCalendar(this)"
                  maxLength=10 size=11 name=overtime>

同一页面中

 function loadCalendar(field){
   var rtn = window.showModalDialog("calendar.jsp","","dialogWidth:290px;dialogHeight:250px;status:no;help:no;scrolling=no;scrollbars=no");
 if(rtn!=null)
  field.value=rtn;
   return;
}

同一级目录下

calendar.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>日期选择器</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
 body,td { font-size:9pt; }
 input
 {
  color:333333;
  font-size:9pt;
  border-width:0;
  padding:2;
  width:20;
 }
</style>
</head>
<script language=javascript>
<!--
    var monthNames = new Array ( "", "1","2","3","4","5","6","7", "8", "9", "10", "11", "12" );
    var endDay = new Array ( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
    var dayNow = 0;
    var monthNow = 0;
    var yearNow = 0;

    function load ( form ) {
       set_month_year_now ();
        var found = false;

        for ( var month=0; month<form.monthList.length; month++ )
            if ( form.monthList[month].text == monthNames[monthNow] ) {
                form.monthList[month].selected = true;
                found = true;
            }

        if ( !found) {
            error ();
            return;
        }

        var found = false;
        for ( var year=0; year<form.yearList.length; year++ )
             if ( form.yearList[year].text == yearNow ) {
                form.yearList[year].selected = true;
                found = true;
             }
        if ( !found) {
            error ();
            return;
        }
        display_month ( form );
    }

    function preceding_month ( form ) {
        var month_selected = form.monthList.selectedIndex;
        var year_selected = form.yearList.selectedIndex;

        if ( !month_selected && !year_selected )  {
            error ();
            return;
        }

        if ( month_selected > 0 )
            month_selected --;
        else {
            month_selected = 11;
            year_selected --;
        }
        form.monthList[month_selected].selected = true;
        form.yearList[year_selected].selected = true;
        display_month ( form );
    }

    function following_month ( form ) {
        var month_selected = form.monthList.selectedIndex;
        var year_selected = form.yearList.selectedIndex;
        if ( month_selected >= ( form.monthList.length - 1 ) && year_selected >= ( form.yearList.length - 1 ) ) {
            error ();
            return;
        }

        if ( month_selected < 11 )
            month_selected ++;
        else {
            month_selected = 0;
            year_selected ++;
        }

        form.monthList[month_selected].selected = true;
        form.yearList[year_selected].selected = true;
        display_month ( form );
    }

    function set_month_year_now ()   {
        var form = document.calendar;
        var now = new Date ();
        monthNow = now.getMonth () + 1;
        yearNow = now.getYear ();
        dayNow = now.getDate();
        yearNow = ( yearNow < 100 ) ? yearNow + 1900 : yearNow;

        var count = 0

        for (var i = yearNow-103; i < yearNow + 50; i++) {
          eval("form.yearList.options[count] = new Option('"+i+"', '"+i+"')");
          count++;
        }
        form.yearList.selectedIndex = 103;
        form.yearList.length = count;
    }

    function error ()   {
        alert ( "超出范围!" );
    }

    function display_month ( form )  {

        var month = form.monthList.selectedIndex + 1;
        var year = parseInt ( form.yearList.options[ form.yearList.selectedIndex].text );
        var start_day = start_day_in_month ( year, month );
        var count = 0;

        for ( var row=0; row<6; row++) {
            for ( var col=0; col<7; col++ )
            {
                if ( row == 0 && col < ( start_day - 1 ) )
                    var day = "";
                else if ( count < endDay[month] )
                    day = ++count;
                else
                    day = "";

                form.dayBox[(row*7)+col].style.display = "";
                form.dayBox[(row*7)+col].style.color = "black";

                if (day == "") {
                  form.dayBox[(row*7)+col].style.display = "none";
                } else {
                 form.dayBox[(row*7)+col].value = day;
                 if (col%7 == 0) form.dayBox[(row*7)+col].style.color = "red";
                 if (yearNow == year && monthNow == month && dayNow == day) form.dayBox[(row*7)+col].style.color = "blue";;
               }
            }
        }
    }

    function start_day_in_month ( year, month )   {
        var  day, daynum, ndays, mnum;
        sday = start_day_in_year ( year );
        endDay[2] = ( year % 4 ) ? 28 : 29;

        if ( month == 1 )
            daynum = sday;
        else {
            ndays = sday;
            for ( mnum=2; mnum<month+1; mnum++ )
                ndays = ndays + endDay[mnum-1];
            daynum = ndays % 7;
        }

        daynum = (!daynum) ? 7 : daynum;
        return (daynum);
    }

    function start_day_in_year ( year )   {
        var y, m, d;
        var n;

        y = year - 1; m = 13; d = 1;
        n = d + 2 * m + ( Math.floor ( ( 0.6 + (m + 1) ) ) + y );
        n = n + Math.floor ( ((y / 4) - Math.floor ( (y / 100 ) ) + Math.floor ( ( y / 400 ) ) )                                                                               ) + 2 ;
        n = Math.floor ( ( (n / 7 - Math.floor ( (n / 7) ) ) * 7 + 0.5 ) );
        return (n+1);
    }

  function CheckDate(strDay) {
   var docFrm = document.calendar;
   var choice_daynum = 0;
   var current_daynum = 0;
   var day_temp;

   if (strDay != "") {
     var strY = docFrm.yearList.value;
     var strM = docFrm.monthList.value;
     var curr_y = new String(yearNow);
     var curr_m = new String(monthNow);
     var curr_d = new String(dayNow);
     if (curr_m.length == 1) curr_m = "0"+curr_m;
     if (curr_d.length == 1) curr_d = "0"+curr_d;
     current_daynum = new Number(curr_y + curr_m + curr_d) ;

     if (strM.length == 1) strM = "0"+strM;
     if (strDay.length == 1) strDay = "0"+strDay;

       choice_daynum = new Number(strY + strM + strDay);
       parent.window.returnValue = strY+"-"+strM+"-"+strDay;  //将选择的日期传递到父窗口中
       parent.window.close();

   }
   return false;
  }
-->
</script>
<body onLoad="load(document.calendar)" topmargin="0">
<center>
  <form name="calendar">
    <table border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="3" height="24"></td>
      </tr>
      <tr>
        <td width="205" nowrap="nowrap" align="right"><select name="yearList" onChange="display_month(this.form)">
          </select>
        </td>
        <td width="65" nowrap="nowrap" align="left"><select name="monthList" size="1" onChange="display_month(this.form)">
            <%for(int i=1;i<13;i++){%>
            <option value="<%=i%>"><%=i%></option>
            <%}%>
          </select>
        </td>
        <td width="10"></td>
      </tr>
      <tr>
        <td colspan="3" height="6"></td>
      </tr>
      <tr>
        <td colspan="3"><table border="0" cellpadding="1" cellspacing="0" align="center">
            <tr>
              <td bgcolor="#82664F"><table border="0" cellpadding="0" cellspacing="0">
                  <tr bgcolor="#82664F" height="18">
                    <td width="31" align="center" nowrap="nowrap"><font color="#FF0000">日</font></td>
                    <td width="31" align="center" nowrap="nowrap">一</td>
                    <td width="31" align="center" nowrap="nowrap">二</td>
                    <td width="31" align="center" nowrap="nowrap">三</td>
                    <td width="31" align="center" nowrap="nowrap">四</td>
                    <td width="31" align="center" nowrap="nowrap">五</td>
                    <td width="31" align="center" nowrap="nowrap">六</td>
                  </tr>
                  <%for(int r=0;r<6;r++){%>
                  <tr bgcolor="#FFFFFF" height="18">
                    <%for(int d=0;d<7;d++){%>
                    <td align="center"><input type="text" size="2" name="dayBox" readOnly onClick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'"  onmouseout="this.style.background='white'">
                    </td>
                    <%}%>
                  </tr>
                  <%}%>
                </table></td>
            </tr>
          </table></td>
      </tr>
    </table>
  </form>
</center>
</body>
</html> 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值