我正在开发一个已经构建的程序,其中包含许多类和jscripts。问题是代码有3个下拉列表的月份,日期,年份。我需要删除日期1并传递静态“01”值。这是在javascript中验证日期的代码 -
function Validate_date(sender, args) {
var m = document.getElementById(sender.id.replace(/cv/, "m"));
var d = document.getElementById(sender.id.replace(/cv/, "d"));
var y = document.getElementById(sender.id.replace(/cv/, "y"));
if (isDate(m.value, d.value, y.value)){
var myDate = new Date();
myDate.setFullYear(y.value, m.value - 1, d.value);
var today = new Date();
today.setDate(today.getDate()-30);
args.IsValid = (myDate >= today);
}
else{
args.IsValid = false;
}
}
现在是isdate函数 -
function isDate(strMonth, strDay, strYear) {
if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1);
if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1);
for (var i = 1; i <= 3; i++) { if (strYear.charAt(0) == "0" && strYear.length > 1) strYear = strYear.substring(1); }
month = parseInt(strMonth);
day = parseInt(strDay);
year = parseInt(strYear);
if (strYear.length != 4 || year == 0) return false;
if (strMonth.length < 1 || month < 1 || month > 12) return false;
if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > DaysArray(12)[month]) return false;
return true;
}
当我给出value =“01”时问题是dropdownlist,它在这一行崩溃了 -
if (isDate(m.value, d.value, y.value)){ in the validate function
dropdownlist code -
<asp:DropDownList ID="EXP_d" runat="server" visible="false">
<asp:ListItem Value="01">DD<ListItem>
<asp:DropDownList>
如何从EXP_d(日期)下拉列表中传递静态值01?
答案 0 :(得分:0)
您可以使用以下内容替换isDate
方法:
function isDate(strMonth, strDay, strYear) {
var month = parseInt(strMonth, 10) - 1;
var day = parseInt(strDay, 10);
var year = parseInt(strYear, 10);
var date = new Date(year, month, day);
return date.getFullYear() === year
&& date.getMonth() === month
&& date.getDate() === day;
}
答案 1 :(得分:0)
在控件上设置visible =“false”意味着ASP.Net不会呈现控件。因此,document.getElementById(sender.id.replace(/cv/, "d"))
将返回null。可以完全删除下拉控件,但这会对后面的代码产生影响。您可以使用<div>
将下拉列表包含在style='display:none'
中以隐藏它,但允许该控件可用于javascript和服务器端代码。