【发布时间】:2015-04-25 22:42:00
【问题描述】:
如果我手动设置command 的值,下面的代码可以工作,但如果我尝试动态获取该值,代码将完全停止工作。我隔离了似乎是问题所在的代码行(在它说它是错误所在之后发表评论)。
如果我评论 alert(document.getElementById("cdmTxt").value); 行,它会按预期工作。
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script type='text/javascript'>
function exec(){
alert("hi");
alert(document.getElementById("cdmTxt").value); //does not seem to execute???
var command = "word";
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var txtToUpdate = document.getElementById("txtHint");
txtToUpdate.innerHTML = txtToUpdate.innerHTML + xmlhttp.responseText + "<br/><br/>";
}
}
xmlhttp.open("GET", "HAS-sync.php?exec=" + command, true);
xmlhttp.send();
}
</script>
<style type="text/css">
.auto-style1 {
margin-top: 0px;
}
</style>
</head>
<body>
<table style="width: 100%">
<tr>
<td style="width: 141px; height: 390px">Managers<br /> DHTs</td>
<td name="targetThing" style="height: 390px">
<form onsubmit="exec();return false">
<input id="cmdTxt" class="auto-style1" name="Text1" type="text">
<input type="submit">
</form>
<br />
<div id="txtHint" style="overflow: scroll; border:1px solid black;width:80%;height:80%"></div>
</td>
</tr>
</table>
</body>
</html>
非常感谢任何帮助!
【问题讨论】:
-
因为您的 id 是
cmdTxt而不是cdmTxt。这应该会在您的控制台上导致错误,例如 trying to read property value of null 或一些类似的消息 -
cmdvscdm打错了。
标签: javascript html