在Javascript中获取文本框文本

时间:2013-05-10 10:36:43

标签: javascript asp.net date

我有一个接受日期值的文本框,例如 01-01-2013 。我需要在javascript中获得一年。这是我用过的代码:

var oldRange, aOldRange;  
var newRange, aNewRange;  
aoldRange = new Array();  
anewRange = new Array();  
var oldYear, newYear;  
oldRange = document.getElementById(
                       '<%= txtAcademicYearStartDate.ClientID%>').value;
aOldRange = oldRange.split("-");
oldYear = aoldRange[2];

但我得到的值是未定义的。有什么问题?

2 个答案:

答案 0 :(得分:5)

您已定义aOldRange,但引用了oldYear = aoldRange[2]

尝试

oldYear = aOldRange[2]

答案 1 :(得分:2)

此示例经过测试且工作正常,请参阅

<script language="javascript" >         
    var oldRange, aOldRange;
    var newRange, aNewRange;
    aoldRange = new Array();
    anewRange = new Array();
    var oldYear, newYear;
    oldRange = '01-01-2013'; 
    aOldRange = oldRange.split("-"); 
    alert(aOldRange[2]);
</script>