[英]Trouble with getting value of html element for php request
我正在尝试使用参数,从页面中HTML元素获取的参数值来进行一些PHP服务器请求。
这是页面代码:
<body>
<div style="margin: 10px 0 20px 0">
<header id="formTitle"></header>
</div>
<div style="display:grid; grid-template-columns: 100px 0.4fr 100px 0.4fr; grid-row-gap: 10px;">
<div class="gridCell"><header>ID</header></div>
<div class="gridCell"><input id="taskID"></div>
<div class="gridCell"><header>Date creation</header></div>
<div class="gridCell"><input id="dateCreation"></div>
<div class="gridCell"><header>Title</header></div>
<div class="gridCell"><input id="title"></div>
<div class="gridCell"><header>Status</header></div>
<div class="gridCell"><input id="status"></div>
<div class="gridCell"><header>Creator</header></div>
<div class="gridCell"><input id="creator"></div>
<div class="gridCell"><header>Responsible</header></div>
<div class="gridCell"><input id="responsible"></div>
<div class="gridCell"><header>Date start</header></div>
<div class="gridCell"><input id="dateStart"></div>
<div class="gridCell"><header>Date finish</header></div>
<div class="gridCell"><input id="dateFinish"></div>
</div>
<div style="margin: 15px 0 5px 0"><header>Description</header></div>
<div><textarea id="description" style="width: 100%; margin: 0 0 0 0" rows="5"></textarea></div>
<div id='commentsTree'>
<script>
document.getElementById('commentsTree').innerHTML = getCommentsTree();
</script>
</div>
<div style="text-align: right; margin: 15px 0 0 0"><button onclick="writeTaskData()">Save</button></div>
</body>
我在使用getCommentsTree();函数时遇到麻烦,这里是:
function getCommentsTree() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost/testapp/php/comments/getCommentsTree.php?ownerID=' + document.getElementById("taskID").value + '&ownerType=task', false);
xhr.send();
if (xhr.status != 200) {
alert( xhr.status + ': ' + xhr.statusText );
}
else {
if(xhr.response != false){
return xhr.response;
}
else{
return "**** you.";
}
}
}
麻烦在这里:
没关系,什么值具有ID为“ taskID”的元素,行
withdocument.getElementById(“ taskID”)。value
总是返回我“”,因此,我的PHP请求总是返回一个空结果。 我做错了什么? 如何为我的PHP请求正确获取值“ taskID”?
我知道了。 撰写此评论:
在调用getCommentsTree时,在输入元素中没有值,该值在文档解析期间被调用
是正确的。 我将我的代码(从服务器获取注释)放在window.onload事件中。 像那样:
var windowTask = window.open("http://localhost/testapp/site/windows/formTask.html", "taskForm");
windowTask.onload = function(){
windowTask.document.getElementById("formTitle").innerText = "Task " + selectedRow.taskID;
windowTask.document.getElementById("taskID").value = selectedRow.taskID;
windowTask.document.getElementById("title").value = selectedRow.title;
windowTask.document.getElementById("status").value = selectedRow.status;
windowTask.document.getElementById("creator").value = selectedRow.creator;
windowTask.document.getElementById("responsible").value = selectedRow.responsible;
windowTask.document.getElementById("description").value = selectedRow.description;
windowTask.document.getElementById("dateCreation").value = selectedRow.dateCreation;
windowTask.document.getElementById("dateStart").value = selectedRow.dateStart;
windowTask.document.getElementById("dateFinish").value = selectedRow.dateFinish;
windowTask.document.getElementById('commentsTree').innerHTML = getCommentsTree(selectedRow.taskID, 'task');
}
并在getCommentsTree()函数中进行一些更改。 这里是:
function getCommentsTree(ownerID, ownerType) {
var xhr = new XMLHttpRequest();
debugger;
xhr.open('GET', 'http://localhost/testapp/php/comments/getCommentsTree.php?ownerID=' + ownerID + '&ownerType=' + ownerType, false);
xhr.send();
if (xhr.status != 200) {
alert( xhr.status + ': ' + xhr.statusText );
}
else {
if(xhr.response != false){
//let commentsTree = JSON.parse(xhr.response);
//debugger;
return xhr.response;
}
else{
return "**** you.";
}
}
}
现在工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.