0

我对 Web 编程非常陌生。目前我尝试使用 JavaScript 获取输入文本值,但不知道为什么只返回未定义的值。请帮忙。非常感谢~~~下面是我的代码:

<!DOCTYPE HTML>
<html>

<head>
<script type="text/javascript">
function CurrentDate(){

var dateobj= new Date();

var date = dateobj.getDate(); 
var day = dateobj.getDay();
var month = dateobj.getMonth();
var year = dateobj.getFullYear();

}

function myScript(){

document.myform.date.value = CurrentDate(date);
document.myform.month.value = CurrentDate(month);
document.myform.day.value = CurrentDate(day);
document.myform.year.value = CurrentDate(year);

}
</script>
</head>


<body onload="myScript()">
<form name="myform" id="myform">
<label for ="date">date: </label>
<input id="date" type="text" name="date">
<label for ="month">month: </label>
<input id="month" type="text" name="month">
<label for ="day">day: </label>
<input id="day" type="text" name="day">
<label for ="year">year: </label>
<input id="year" type="text" name="year">

</form> 

</body>
</html>
4

4 回答 4

3

javascript 具有功能范围,因此以下是您的代码的情况

function CurrentDate(){
    // everything being declared in here is only
    // going to exist until the end of the function
    var dateobj= new Date();
    var date = dateobj.getDate(); 
    var day = dateobj.getDay();
    var month = dateobj.getMonth();
    var year = dateobj.getFullYear();

}

function myScript(){
    // javascript won't die if you pass in 
    // parameters, they will either be used or ignored by
    // the method that is called
    document.myform.date.value = CurrentDate(date);
    document.myform.month.value = CurrentDate(month);
    document.myform.day.value = CurrentDate(day);
    document.myform.year.value = CurrentDate(year);

}

您可能想要做的是删除当前日期函数并使您的代码看起来像这样。

function myScript(){
    var dateobj= new Date();
    document.myform.date.value = dateobj.getDate();
    document.myform.month.value = dateobj.getMonth();
    document.myform.day.value = dateobj.getDay();
    document.myform.year.value = dateobj.getFullYear();

}

我的 jsfiddle 展示了这些更改将如何工作
Douglas Crockford 的网页将为您提供了解更多关于 javascript 的资源。

于 2013-10-12T03:27:41.607 回答
0

你试一下...

<head>
    <script type="text/javascript">
        function CurrentDate(params) {
            var dateobj = new Date();
            if (params == date) {
                return dateobj.getDate();
            } else if (params == month) {
                return dateobj.getMonth();
            } else if (params == year) {
                return dateobj.getFullYear();
            } else if (params == day) {
                return dateobj.getDay();
            } else {
                alert('Input invalid !');
            }
        }

        function myScript() {

            document.myform.date.value = CurrentDate(date);
            document.myform.month.value = CurrentDate(month);
            document.myform.day.value = CurrentDate(day);
            document.myform.year.value = CurrentDate(year);

        }
    </script>
</head>


<body onload="myScript()">
    <form name="myform" id="myform">
        <label for ="date">date: </label>
        <input id="date" type="text" name="date">
        <label for ="month">month: </label>
        <input id="month" type="text" name="month">
        <label for ="day">day: </label>
        <input id="day" type="text" name="day">
        <label for ="year">year: </label>
        <input id="year" type="text" name="year">

    </form> 

</body>

于 2013-10-12T03:38:36.087 回答
-1

尝试document.getElementByID("date").value获取价值。

还可以考虑使用 jquery 使其更容易$("#date").val() 获取或设置输入值。

于 2013-10-12T03:25:41.870 回答
-1
document.getElementById('date').value; // Use this to get your value.
于 2013-10-12T03:28:27.343 回答