gpt4 book ai didi

javascript - 获取元素 id 时出现问题(未捕获的 TypeError : Cannot set property 'innerHTML' of null )

转载 作者:行者123 更新时间:2023-11-28 20:38:14 25 4
gpt4 key购买 nike

我想从 div 获取一些值并将其发送到数据库,我编写了它并且它在我的本地主机上工作,但是当我将源代码上传到我的主机上时它停止工作......

我在控制台中遇到这个错误

Uncaught TypeError: Cannot set property 'innerHTML' of null index.php:700
request.onreadystatechange

ajax 位于 div 所在的同一页面

   <script type="text/javascript">
// create the XMLHttpRequest object, according browser
function get_XmlHttp() {
// create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
var xmlHttp = null;

if(window.XMLHttpRequest) { // for Forefox, IE7+, Opera, Safari, ...
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject) { // for Internet Explorer 5 or 6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

return xmlHttp;
}

// sends data to a php file, via POST, and displays the received answer
function ajaxrequest(php_file, tagID) {
var request = get_XmlHttp(); // call the function for the XMLHttpRequest instance

// create pairs index=value with data that must be sent to server
var the_data = 'pg='+document.getElementById('template').innerHTML;
var the_data2 = 'memid='+document.getElementById('memid').innerHTML;
var the_data3 = 'proid='+document.getElementById('proid').innerHTML;
var the_data4 = 'taipei='+document.getElementById('taipei').innerHTML;

request.open("POST", php_file, true); // set the request

// adds a header to tell the PHP script to recognize the data as is sent via POST
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

var post_data = the_data + '&' + the_data2 + '&' + the_data3 + '&' + the_data4;

request.send(post_data); // calls the send() method with datas as parameter
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
request.onreadystatechange = function() {
if (request.readyState == 4) {
document.getElementById(tagID).innerHTML = request.responseText;
}
}
}
</script>

div

<div id="template"> some html here </div>
<div id="memid" style="display:none;">4</div>
<div id="proid" style="display:none;">55</div>
<div id="taipei" style="display:none;">sav</div>

按钮

<button value="sasasasasa" onclick="ajaxrequest('temp1.php', 'context')"></button>

最佳答案

假设第 700 行是

document.getElementById(tagID).innerHTML = request.responseText;

...那么您显然在 tagID 中没有带有 id 的元素,根据您显示的示例按钮,该元素将是 “上下文”。事实上,您还没有显示任何使用 id "context" 定义元素的 HTML。

关于javascript - 获取元素 id 时出现问题(未捕获的 TypeError : Cannot set property 'innerHTML' of null ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14911240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com