从文本框中获取值并显示结果

时间:2016-11-28 08:14:07

标签: javascript html

如何从文本框中获取价值,当我点击按钮时,我的功能适用于文本框值 这个程序用于计算年龄

function getDate(birth){
    var now =  new Date();
    var currentYear = now.getFullYear();
    var currentMonth = now.getMonth();
    var currentDay = now.getDate();
    
    var birthYear = birth.getFullYear();
    var birthMonth = birth.getMonth();
    var birthDay = birth.getDate();
    
    var age = currentYear - birthYear;
    var age_month = currentMonth - birthmonth;
}

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="javascript.js"></script>
    </head>
    <body>
        <form>
            <input type="text"/>
            <input type="button" value="Calc" onclick="getAge()"/>
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
function getAge(){
  var text=document.getElementById('myInput').value;
  console.log(text);
  }
&#13;
<input id="myInput" type="text"/>
<input type="button" value="Calc" onclick="getAge()"/>
&#13;
&#13;
&#13;