【发布时间】:2013-07-05 19:17:11
【问题描述】:
我搜索了一个可能的答案并浏览了一些帖子,包括 How to fix "Uncaught TypeError: Cannot call method 'appendChild' of null" 和 Receiving "InvalidStateError: DOM Exception 11" during websocket.send 但似乎都没有解决我的问题。 将数据从 html 表单保存到 mysql 数据库的保存按钮工作得非常好,直到页面开始出现两个错误
页面上的下拉菜单使用 ajax 从数据库中动态更新。它们仍然工作正常,并根据所选选项自动更新,但是当我单击任何下拉菜单时,错误控制台显示
"Uncaught error: InvalidStateError: DOM Exception 11 on createoptions.js line 37即xmlhttp.send();当我单击保存按钮时,没有任何响应或将数据保存到数据库,而是显示一个错误,上面写着
Uncaught TypeError: Cannnot call method "submit" of null。需要注意的是,调用 submit() 方法的对象已经存在。我使用了document.getElementById('studentdata').submit()",并且表格studentdata存在于同一页面上。
请强调这些错误的原因以及我可以做些什么来解决它们。以下是相关代码。
createoptions.js
function setdepartment(value){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("departmentselect1").innerHTML=xmlhttp.responseText;
}
}
switch(value){
case "Allied Health Sciences" : xmlhttp.open("GET","deptahs.php",true); break;
case "Engineering and Inter-disciplinary sciences" : xmlhttp.open("GET","depteids.php",true); break;
case "HIMSR" : xmlhttp.open("GET","depthimsr.php",true); break;
case "Islamic Studies and Social Sciences" : xmlhttp.open("GET","deptisss.php",true); break;
case "Management and Information Technology" : xmlhttp.open("GET","deptmanagement.php",true); break;
case "Medicine (Unani)" : xmlhttp.open("GET","deptmedicine.php",true); break;
case "Nursing" : xmlhttp.open("GET","deptnursing.php",true); break;
case "Pharmacy" : xmlhttp.open("GET","deptpharmacy.php",true); break;
case "Science" : xmlhttp.open("GET","deptscience.php",true); break;
}
xmlhttp.send();
}
page.php 和被引用的行是<li><a class="button black" form="studentdata" onclick="document.getElementById('studentdata').submit()">Save</a></li>
<div class="navbutton">
<aside>
<ul>
<li><a class="button black" href="logout.php">Logout</a></li>
<li><a class="button black" onclick="addRow()">Add Row</a></li>
<li><a href="#searchbox" class="button black" onclick="showsearch()">Search</a></li>
<li><a href="#promotediv" class="button black" onclick="showpromote()">Promote</a></li>
<li><a class="button black" form="studentdata" onclick="document.getElementById('studentdata').submit()">Save</a></li>
</ul>
</aside>
</div>
<form id="studentdata" action="savedata.php" method="POST">
<div style=" padding-left:350px; ">
<div class="dropdown dropdown-dark">
<select name="facultyselect1" id="facultyselect1" style="width:300px;" class="dropdown-select " onfocus="setdepartment(this.value)" onchange="setdepartment(this.value)" required>
<option value="">Select an option</option>
<div id="facultyselect1">
<?php
@ $db = mysql_connect("localhost", "root", "");
mysql_select_db("university");
$strSQL = "SELECT facultyname FROM faculty";
$rs = mysql_query($strSQL);
$nr = mysql_num_rows($rs);
for ($i=0; $i<$nr; $i++) {
$r = mysql_fetch_array($rs);
echo "<OPTION VALUE=\"".$r["facultyname"]."\">".$r["facultyname"]."</OPTION>";
}
?>
</div>
</SELECT>
</div>
<div class="dropdown dropdown-dark">
<select name="departmentselect1" id="departmentselect1" class="dropdown-select" onchange="setcourse(this.value)" onfocus="setcourse(this.value)" required>
<option value="">Select an option</option>
<div id="departmentselect1">
</div>
</select>
</div>
</div>
</form>
【问题讨论】:
-
可以显示 savedata.php 但我认为不需要。
-
在
setdepartment函数中执行console.log( value );。它实际上是否匹配您的任何“案例”值? -
我在第二个下拉列表中得到了值,如果 setdepartment 函数工作正常的话。我更关心的是点击提交按钮时出现的第二个错误。
-
我注意到两个具有相同 id 的元素。 id 应该始终是唯一的,因此请更改 id 为
departmentselect1的元素之一。 -
是的,但认真地使用相同的 id,我之前能够将数据保存到数据库中。 `Cannnot call method "submit" of null`的可能原因是什么。
标签: javascript html dom