如何使用JS获取输入文本值

时间:2013-10-12 03:20:39

标签: javascript html

我是网络编程的新手。目前我尝试使用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 个答案:

答案 0 :(得分: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();

}

My jsfiddle showing how the changes would work
Douglas Crockford's web page将为您提供了解有关javascript的更多信息的资源。

答案 1 :(得分: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>

答案 2 :(得分:-1)

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

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

答案 3 :(得分:-1)

document.getElementById('date').value; // Use this to get your value.