我正在制作一个从MySQLi数据库结果按程序生成HTML代码的应用程序。它使用一个调用JavaScript函数的按钮,该函数将XMLHttpRequest发送到服务器,服务器将删除该行以在处理完项目后从列表中删除它们。以下是相关的PHP代码:
<?php
require_once("lib/dbclass.php");
$conn = new Conn();
$result = $conn->query("SELECT * FROM orders");
?>
<?php foreach($result as $item) {?>
<div class="row" id="item<?php echo $item['oid']; ?>">
<p>Type</p>
<span class="item"><?php echo htmlspecialchars($item['a']); ?></span>
<p>Size</p>
<span class="item"><?php echo htmlspecialchars($item['b']); ?></span><br>
<button type="button" onclick="close('<?php echo $item['c']; ?>')">Item processed</button>
</div>
<?php }?>
<script src="js/dashboard.js"></script>
这是dashboard.js:
function close(order_id) {
console.log("Closing order number " + order_id);
var http = new XMLHttpRequest();
http.onreadystatechange = function() {
console.log("Readystate changed! New: " + this.readyState);
if (this.readyState == 4 && this.status == 200) {
console.log("Operation completed.");
document.getElementById("order" + order_id).remove();
}
}
http.open("GET", "order_complete.php?oid=" + order_id, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send();
}
但是,问题在于onclick不会调用close函数。在检查元素面板中,它正确显示为<button type="button" onclick="close(2)")>Item processed</button>
。但是,当我单击按钮时,什么也没有发生。我试过了:
type="button"
。没有区别。onclick
标记中删除参数,使其看起来像onclick="close(1)"
。没有区别。onclick
按钮的从检查面板更改为alert("Hi")
。按下按钮时调用的函数。当我将其更改回close(2)
它时alert("Hi")
再次调用。已解决:当然close()
是保留功能。对其进行更改以closeOrder()
解决此问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句