我无法在 JavaScript 中获取要打印到文本区域的属性值,我收到一个错误:未捕获的类型错误:无法读取 null 的

I can't get the property value to print to textarea in JavaScript, I get an error: Uncaught TypeError: Cannot read property 'value' of null

本文关键字:错误 一个 读取 null 类型 获取 JavaScript 打印 文本 属性 区域      更新时间:2023-09-26

我正在努力为我的在线考试申请获取打印到文本区域的值。我是 JavaScript 的新手,但学得很快。我欢迎任何帮助,因为它让我疯狂地尝试各种方式但没有成功。提前谢谢。

网页代码:


<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
         <title>testexam</title>
             <link rel="stylesheet" href="tutor.css" media="all" type="text/css">               
                         <!-- Custom Fonts -->
             <link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
             <!-- JS code from stack overflow; modified to suit application by Paul Gillis.-->
             <script type="text/javascript"></script>
</head> 
      <body>
           <div class= "container">
                <div class="col-sm-4">
                      <img class="img- img-responsive img-center" src="img/EXAM.PNG" alt="">
                </div>  
          </div>
  <h1>Exam Level 1</h1>
      <form name ="testexam">
          <ol>
                <div><img class="img- img-responsive img-center" src="examimg/A1sharp.png" alt=""></div>
                     <li>Question 1, which is the correct answer</li>   
        <ul> 
            <li><input type = "radio" name ="q1" value = "wrong" >This note is B</input></li>
            <li><input type = "radio" name ="q1" value = "correct" >This note is A#</input></li>
            <li><input type = "radio" name ="q1" value = "wrong" >This note is C</input></li>
        </ul>
            <hr>
  <div><img class="img- img-responsive img-center" src="examimg/A1.png" alt=""></div>           
       <li> Question 2, which is the correct answer</li>
           <ul>
               <li><input type = "radio" name ="q2" value = "correct">This note is A</input></li>
                   <li><input type = "radio" name ="q2" value = "wrong">This note is B</input></li>
                       <li><input type = "radio" name ="q2" value = "wrong">This note is C</input></li>
           </ul>
                <hr>
    <div><img class="img- img-responsive img-center" src="examimg/B1.png" alt=""></div>
      <li> Question 3, which is the correct answer</li>
        <ul>
            <li><input type = "radio" name ="q3" value = "wrong">This note is D</input></li>
            <li><input type = "radio" name ="q3" value = "correct">This note is B</input></li>
            <li><input type = "radio" name ="q3" value = "wrong">This note is A</input></li>
        </ul>
            <hr>
                <input type ="button" value = "hello" onclick="sayhello()"></input>
                <input type ="button" value = "submit" onclick ="checkAll()"></input>
                <input type ="reset" value = "clear"></input>
                    <textarea rows ="6" cols = "60" name ="answerswersBox">Your Answer Results are Listed   Below</textarea>
</body>
    <footer>
        <script type="text/javascript" src="js/testexam.js"></script>
    </footer>
</html>

Javacript 代码

function sayhello()
   {
       alert("hello");
   }
var text2display = "";
    var answers = new Array(3);
        answers[0] = "1.This note is A#'n";
        answers[1] = "2.This note is A'n";
        answers[2] = "3.This note is B'n";

var questions = new Array(3);
    questions[0] = "q1";
    questions[1] = "q2";
    questions[2] = "q3";

function checkQs(s)
{
    var qs = document.getElementsByName(s);
        var noOfRadios = qs.length;
            for(var i = 0; i < noOfRadios; i++)
    {
              if(qs[i].checked)
        {
                  if (qs[i].value == "correct")
                      text2display = text2display + "Well Done You are Correct";
                          else text2diaplay = text2display + answers[questions.indexOf(s)];
            break;      
        }       
    }
}
   function checkAll()
{
   for(var i = 0; i < questions.length; i++)
{
     checkQs(questions[i]);
}
        testexam.answerBox.value = text2display;
}

您给出了错误的文本区域名称:使用此:您的文本区域名称为answersBox . 并且您还需要关闭表单标记。您的 HTML 组织得不好 尝试遵循一些教程。

testexam.answersBox.value = text2display;

  function sayhello()
        {
            alert("hello");
          
        }
     var text2display = "";
         var answers = new Array(3);
             answers[0] = "1.This note is A#'n";
             answers[1] = "2.This note is A'n";
             answers[2] = "3.This note is B'n";
     var questions = new Array(3);
         questions[0] = "q1";
         questions[1] = "q2";
         questions[2] = "q3";
     function checkQs(s)
     {
         var qs = document.getElementsByName(s);
             var noOfRadios = qs.length;
                 for(var i = 0; i < noOfRadios; i++)
         {
                   if(qs[i].checked)
             {
                       if (qs[i].value == "correct")
                           text2display = text2display + "Well Done You are Correct";
                               else text2diaplay = text2display + answers[questions.indexOf(s)];
                 break;      
             }       
         }
     }
        function checkAll()
     {
        for(var i = 0; i < questions.length; i++)
     {
          checkQs(questions[i]);
     }
        testexam.answersBox.value = text2display;
     }
<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
         <title>testexam</title>
             <link rel="stylesheet" href="tutor.css" media="all" type="text/css">               
                         <!-- Custom Fonts -->
             <link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
             <!-- JS code from stack overflow; modified to suit application by Paul Gillis.-->
             <script type="text/javascript"></script>
</head> 
      <body>
           <div class= "container">
                <div class="col-sm-4">
                      <img class="img- img-responsive img-center" src="img/EXAM.PNG" alt="">
                </div>  
          </div>
  <h1>Exam Level 1</h1>
      <form name ="testexam">
          <ol>
                <div><img class="img- img-responsive img-center" src="examimg/A1sharp.png" alt=""></div>
                     <li>Question 1, which is the correct answer</li>   
        <ul> 
            <li><input type = "radio" name ="q1" value = "wrong" >This note is B</input></li>
            <li><input type = "radio" name ="q1" value = "correct" >This note is A#</input></li>
            <li><input type = "radio" name ="q1" value = "wrong" >This note is C</input></li>
        </ul>
           
  <div><img class="img- img-responsive img-center" src="examimg/A1.png" alt=""></div>           
       <li> Question 2, which is the correct answer</li>
           <ul>
               <li><input type = "radio" name ="q2" value = "correct">This note is A</input></li>
                   <li><input type = "radio" name ="q2" value = "wrong">This note is B</input></li>
                       <li><input type = "radio" name ="q2" value = "wrong">This note is C</input></li>
           </ul>
    <div><img class="img- img-responsive img-center" src="examimg/B1.png" alt=""></div>
      <li> Question 3, which is the correct answer</li>
        <ul>
            <li><input type = "radio" name ="q3" value = "wrong">This note is D</input></li>
            <li><input type = "radio" name ="q3" value = "correct">This note is B</input></li>
            <li><input type = "radio" name ="q3" value = "wrong">This note is A</input></li>
        </ul>
             </ol>
                <input type ="button" value = "hello" onclick="sayhello()"></input>
                <input type ="button" value = "submit" onclick ="checkAll()"></input>
                <input type ="reset" value = "clear"></input>
                <textarea rows ="6" cols = "60" name ="answerswersBox">Your Answer Results are Listed   Below</textarea>
               
                </form>
</body>
    <footer>
        <script type="text/javascript">
        
        </script>
    </footer>
</html>               		

相关文章: