javascript如何把用户输入的年月日信息,从不可编辑的文本框里显示出来?

img

img

<form name = "datetable">
  <table>
    <tr>
      <td>Enter year(e.g. 2022):</td>
      <td><input type="text" name="year" value=""></td>
    </tr>
    <tr>
      <td>Enter month(1 to 12):</td>
      <td><input type="text" name="month" value=""></td>
    </tr>
    <tr>
      <td>Enter day(1 to 31):</td>
      <td><input type="text" name="day" value=""></td>
    </tr>
    <tr>
      <td><input type="button" value="Display Date" onclick=" document.output.value="display()" "></td> //主要问题是如何把Script里面的内容通过按钮可以传送到文本框里打印出来
    </tr>
    <tr>
      <td><textarea name="output" rows=5 cols=50  value="" readonly>
      </textarea></td>
    </tr>
  </table>
  <script>
    var year = document.datetable.year.value;
    var month =document.datetable.month.value;
    var day =document.datetable.day.value;
    var d1 = new Date(year, month, day);
    function display(){
    document.write("The Year:" + d1.getFullYear() + "<br/>");
    document.write("The month:" + d1.getMonth() + "<br/>");
    document.write("The day:" + d1.getDay() + "<br/>");
    document.write("The weekday:" + d1.getDate() + "<br/>");
    }
  </script>

<form name = "datetable">
  <table>
    <tr>
      <td>Enter year(e.g. 2022):</td>
      <td><input type="text" name="year" value=""></td>
    </tr>
    <tr>
      <td>Enter month(1 to 12):</td>
      <td><input type="text" name="month" value=""></td>
    </tr>
    <tr>
      <td>Enter day(1 to 31):</td>
      <td><input type="text" name="day" value=""></td>
    </tr>
    <tr>
      <td><input type="button" value="Display Date" onclick=" document.output.value="display()" "></td> //主要问题是如何把Script里面的内容通过按钮可以传送到文本框里打印出来
    </tr>
    <tr>
      <td><textarea name="output" rows=5 cols=50  value="" readonly>
      </textarea></td>
    </tr>
  </table>
  <script>
    var year = document.datetable.year.value;
    var month =document.datetable.month.value;
    var day =document.datetable.day.value;
    var d1 = new Date(year, month, day);
    function display(){
      const textarea = document.getElementsByTagName('textarea')[0]
      var time = "The Year:" + d1.getFullYear() + "\nThe month:" + d1.getMonth() + "\nThe day:" + d1.getDay() + "\nThe weekday:" + d1.getDate() + "\n"
       textarea.innerHTML = time
    }
  </script>
 

img


<!--
 * @Author: your name
 * @Date: 2021-12-30 16:59:23
 * @LastEditTime: 2022-03-08 09:23:06
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /未命名文件夹/123.html
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <form name = "datetable">
    <table>
      <tr>
        <td>Enter year(e.g. 2022):</td>
        <td><input type="text" name="year" id="year" value="" ></td>
      </tr>
      <tr>
        <td>Enter month(1 to 12):</td>
        <td><input type="text" name="month" id="month" value=""></td>
      </tr>
      <tr>
        <td>Enter day(1 to 31):</td>
        <td><input type="text" name="day" id="day" value=""></td>
      </tr>
      <tr>
        <td><input type="button" value="Display Date" id="btnsubmit"></td> //主要问题是如何把Script里面的内容通过按钮可以传送到文本框里打印出来
      </tr>
      <tr>
        <td><textarea name="output" id="output" rows=5 cols=50  value="" readonly>
        </textarea></td>
      </tr>
    </table>
    <script>
      document.querySelector('#btnsubmit').onclick=function(){
        var year =  document.querySelector('#year').value
       var month = document.querySelector('#month').value
       var day =  document.querySelector('#day').value
       var monthDay=new Date(`${year}-${month}-${day}`).getDay();
        var weeks=new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
        var week=weeks[monthDay];

       document.querySelector('#output').value = `
       The Year:${year}\n
       The month:${month}\n
       The day:${day}\n
       The weekday:${week}\n
`
      }
  
 

    </script>
  
</body>
</html>

这个是自问自答?双向数据绑定他不香?

DOM操作:获取DOM,设置innerText

获取到input值 拼接起来 赋值给 textarea的 innerText