Php 当我试图在确认中显示数据时,我总是得到一个未定义的
我有以下javascript和php/html代码: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
<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();
}
}