javascript - 如何通过javascript获取部门ID并通过ajax发送到php文件?
问题描述
嘿,我正在研究一个项目和通知领域。一切顺利,但我遇到了一个问题,我想通过 ajax 将通知 ID 发送到 php 文件以使其为已读。
即我发出通知以显示为具有自己ID的部门
<div id=‘<?php echo $n_id ; ?>’class=‘notifications’>
<div class=“head”>
Hi Users Party Is On</div>
</div>
我想添加 onclick 属性,以便当用户单击该属性时,触发javascript函数并通过ajax将id发送到php文件并运行代码,并且通知将被标记为已读。
<div id=‘<?php echo $id; ?>’ onclick=‘update(this.id)’ class=‘notifications’>
<div class=‘head’>Hi Users Party Is On?</div>
</div>
我的ajax代码是
function update(str) {
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("mpk").innerHTML=this.responseText;
}
}
xmlhttp.open("GET",”update.php?value="+str,true);
xmlhttp.send();
}
解决方案
您可以在函数中传递this
ie : current div clicked reference,然后使用variable.id
.
演示代码:
function update(str) {
var ids = str.id;
console.log(ids)
//send "ids" ajax call
}
<div id="1" onclick="update(this)" class="notifications">
<div class="head">Hi Users Party Is On?</div>
</div>
<div id="2" onclick="update(this)" class="notifications">
<div class="head">Hi Users Party Is On?</div>
</div>