嘿,我正在处理一个项目和通知字段。一切进展顺利,但我遇到了一个问题,我想通过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();
}发布于 2020-07-30 16:04:53
你可以在你的函数中传递this,也就是:当前div点击的引用,然后使用variable.id获取该div的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>
https://stackoverflow.com/questions/63168501
复制相似问题