html:
<table> <tr> <td align="right" width="120px"> 销售时间区间: </td> <td> <select id="ddlChoiceDate" name="ddlChoiceDate" runat="server" onchange="SelectChange(this.value);"> <option value="选择时间" selected>选择时间</option> <option value="今天">今天</option> <option value="本周">本周</option> <option value="本月">本月</option> <option value="今年">今年</option> </select> </td> <td align="left"> <input type="text" id="txtOrd_CreateTimeStart" runat="server" width="85px" onfocus="DateTimeStart(this,'txtOrd_CreateTimeEnd');" disabled="" /> 至 <input type="text" id="txtOrd_CreateTimeEnd" runat="server" width="85px" onfocus="DateTimeEnd(this,'txtOrd_CreateTimeStart');" disabled="" /> </td> </tr> </table>
js:
js: <script> //根据销售时间区间下拉列表框的选择判断时间截止文本框是否可用 function TxtDisabled() { if ($("#ddlChoiceDate").val() == "选择时间") { $("#txtOrd_CreateTimeStart").removeAttr("disabled"); //可用 $("#txtOrd_CreateTimeEnd").removeAttr("disabled"); //可用 } else { $("#txtOrd_CreateTimeStart").attr("disabled", "disabled"); //禁用 $("#txtOrd_CreateTimeEnd").attr("disabled", "disabled"); //禁用 } } $(document).ready(function () { TxtDisabled(); }); function SelectChange(selVal) { TxtDisabled(); switch (selVal) { case "选择时间": $("#txtOrd_CreateTimeStart").val(""); //txtOrd_CreateTimeStart $("#txtOrd_CreateTimeEnd").val(""); //txtOrd_CreateTimeEnd break; case "今天": $("#txtOrd_CreateTimeStart").val(getThisDayTime("txtOrd_CreateTimeStart")); //txtOrd_CreateTimeStart $("#txtOrd_CreateTimeEnd").val(getThisDayTime("txtOrd_CreateTimeEnd")); //txtOrd_CreateTimeEnd break; case "本周": $("#txtOrd_CreateTimeStart").val(getWeekTime("txtOrd_CreateTimeStart")); //txtOrd_CreateTimeStart $("#txtOrd_CreateTimeEnd").val(getWeekTime("txtOrd_CreateTimeEnd")); //txtOrd_CreateTimeEnd break; case "本月": $("#txtOrd_CreateTimeStart").val(getMonthTime("txtOrd_CreateTimeStart")); //txtOrd_CreateTimeStart $("#txtOrd_CreateTimeEnd").val(getMonthTime("txtOrd_CreateTimeEnd")); //txtOrd_CreateTimeEnd break; case "今年": $("#txtOrd_CreateTimeStart").val(getYearTime("txtOrd_CreateTimeStart")); //txtOrd_CreateTimeStart $("#txtOrd_CreateTimeEnd").val(getYearTime("txtOrd_CreateTimeEnd")); //txtOrd_CreateTimeEnd break; default: break; } } var now = new Date();//获取当前时间 var Year = now.getFullYear();//得到年 var Month = now.getMonth() + 1; //得到月 //getMonth()是以0开始的月份 //当前月值(1月=0,12月=11) var Day = now.getDate(); //得到日 var GDay = now.getDay();//得到星期几 0:星期日 1:星期一 2:星期二 3:星期三 4:星期四 5:星期五 6:星期六 function getThisDayTime(id) { var beginTime = ""; if (Day < 10) { beginTime = Year + "-" + Month + "-0" + Day; //格式 Y-m-d } else { beginTime = Year + "-" + Month + "-" + Day; //格式 Y-m-d } return beginTime; } //计算本周起始日期,并以 Y-m-d 形式返回。 function getWeekTime(id) { var Dy = Day - GDay; if (GDay == 0) { Dy -= 7; } if (id == "txtOrd_CreateTimeEnd") { Dy += 7; } var beginTime = ""; if (Dy < 10) { beginTime = Year + "-" + Month + "-0" + Dy; //格式 Y-m-d } else { beginTime = Year + "-" + Month + "-" + Dy; //格式 Y-m-d } return beginTime; } //计算本月开始时间,并以Y-m-d 形式返回 function getMonthTime(id) { var Dy = 1; if (id == "txtOrd_CreateTimeEnd") { Dy = solarDays(Year, Month); } var beginTime = ""; if (Dy < 10) { beginTime = Year + "-" + Month + "-0" + Dy; //格式 Y-m-d } else { beginTime = Year + "-" + Month + "-" + Dy; //格式 Y-m-d } return beginTime; } //计算今年开始时间,并以Y-m-d 形式返回 function getYearTime(id) { var beginTimes = ""; if (id == "txtOrd_CreateTimeStart") { beginTimes = Year + "-01-01"; //格式 Y-m-d } else if (id == "txtOrd_CreateTimeEnd") { beginTimes = Year + "-12-31"; //格式 Y-m-d } return beginTimes; } //当月的天数 function solarDays(y, m) { var solarMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if (m == 2) return (((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)) ? 29 : 28); else return (solarMonth[m - 1]); } </script>