Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当我试图在确认中显示数据时,我总是得到一个未定义的_Php_Javascript_Jquery - Fatal编程技术网

Php 当我试图在确认中显示数据时,我总是得到一个未定义的

Php 当我试图在确认中显示数据时,我总是得到一个未定义的,php,javascript,jquery,Php,Javascript,Jquery,我有以下javascript和php/html代码: <script> var examInput = document.getElementById('newAssessment').value; var dateInput = document.getElementById('newDate').value; var timeInput = document.getElementById('newTime').value

我有以下javascript和php/html代码:

<script>
          var examInput = document.getElementById('newAssessment').value;
          var dateInput = document.getElementById('newDate').value;
          var timeInput = document.getElementById('newTime').value;

         function showConfirm(){

         var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: " + examInput +  "\n" + "Date: " + dateInput + "\n" + "Time: " + timeInput);

         if (confirmMsg==true)
         {
         submitform();   
     }
}

</script>

var TestPut=document.getElementById('newAssessment')。值;
var dateInput=document.getElementById('newDate')。值;
var timeInput=document.getElementById('newTime')。值;
函数showConfirm(){
var confirmsg=confirm(“是否确实要更新以下内容:”+“\n”+”检查:“+testput+”\n”+”日期:“+dateInput+”\n”+”时间:“+timeInput”);
如果(confirmMsg==true)
{
submitform();
}
}

标记在标记中遇到时立即执行。我们假设您的
标记出现在它在
getElementById()
调用中引用的HTML之前

由于设置变量的三行出现在它们的任何函数之外,并在加载DOM之前立即执行,因此相应的DOM元素还不存在,并且变量变得
未定义
。只需将它们移动到函数中。这样做的另一个好处是,在以后调用函数时,这些值都是最新的

 function showConfirm(){
   // Load these variables inside the function
   // If placed outside, their DOM nodes are not yet loaded when the script runs
   var examInput = document.getElementById('newAssessment').value;
   var dateInput = document.getElementById('newDate').value;
   var timeInput = document.getElementById('newTime').value;

   var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: " + examInput +  "\n" + "Date: " + dateInput + "\n" + "Time: " + timeInput);

   if (confirmMsg)
   {
     submitform();   
   }
 }

当你先回答时,你会得到最好的答案:)
 function showConfirm(){
   // Load these variables inside the function
   // If placed outside, their DOM nodes are not yet loaded when the script runs
   var examInput = document.getElementById('newAssessment').value;
   var dateInput = document.getElementById('newDate').value;
   var timeInput = document.getElementById('newTime').value;

   var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: " + examInput +  "\n" + "Date: " + dateInput + "\n" + "Time: " + timeInput);

   if (confirmMsg)
   {
     submitform();   
   }
 }