有以下代码:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div id="Block"></div>
<script type="text/javascript">
function Request(Mode)
{
jQuery.ajax
({
url: "Script.php",
type: "POST",
dataType: "html",
data: "Mode=" + Mode,
success: function(responce)
{
if (responce == '1')
{
document.getElementById("MessageField").innerHTML = "Message text";
}
else
{
document.getElementById("Block").innerHTML = responce;
}
},
error: document.getElementById("Block").innerHTML = "Request error."
});
}
Request("load form");
</script>
</body>
</html>
php-script返回html-form或值'1':
<?php
if ($_POST['Mode'] == 'load form')
echo
"
<form id = 'AuthForm' method = 'post' action = ''>
<table border = 1>
<tr>
<td id = 'MessageField'>
 
</td>
</tr>
<tr>
<td>
<input type = 'submit' name='B1' id='B1' value='Show message' onclick = 'Request()'>
</td>
</tr>
</table>
</form>
";
else echo "1";
?>
Javascript函数输出html-form或更改其id =“MessageField”的表格单元格的innerHTML。 表单显示为假设,但按下按钮我收到错误:
Uncaught TypeError: Cannot set property 'innerHTML' of null
脚本无法执行字符串:
document.getElementById("MessageField").innerHTML = "Message text";
为什么呢?当脚本尝试更改它时,MessageField已存在。 O_O
答案 0 :(得分:1)
您指定的代码段中没有任何元素具有ID“Block”