<div id = 'eventcontainer' >
<span id = 'class'>
<?php
//Getting posts from DB
$event1 = mysql_query("SELECT post,date,memid FROM postaction WHERE memid = '$id' ORDER BY date DESC LIMIT 5;");
while ($row1 = mysql_fetch_array($event1))
{
$event = $row1['post'];
$timeposted = $row1['date'];
$eventmemdata = mysql_query("SELECT id,firstname FROM users WHERE id = '$id' LIMIT 1");
while($rowaa = mysql_fetch_array($eventmemdata))
{
$name = $rowaa['firstname'];
$eventlist = "$event <br> $name";
}
echo " <div id = 'eventer'> $timeposted <br>$eventlist</div> <input name='myBtn' type='submit' value='increment' onClick='javascript:ajax_post();'>
<input name='lol' type='submit' value='dec' onClick='javascript:ajax_posta();'>
<div id = 'status'>lol</div>";
echo "<br>";
}
$('.eventcontainer.button').click(function() {
$.post('javas.php', function(data) {
$(this).parent('div').find('.status').html(data);
})
});
?>
</div>
</span>
尝试运行此代码时出错。 我想要做的是尝试读取单击该按钮的div / class,然后仅在该类中执行相对的ajax / jquery功能。
这是Ajax / Jquery function ajax_post(){
$('.eventcontainer.button').click(function() {
$.post('javas.php', function(data) {
$(this).parent('div').find('.status').html(data);
})
});
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send("num=" + (++num)); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
答案 0 :(得分:0)
正如评论所指出的那样,你的PHP标签中不应该有jQuery。将一些脚本脚本标记添加到页面的头部,添加document.ready处理程序,然后将脚本放在那里。
请注意,一旦完成此操作,您仍然会遇到一些问题,因为您的this
值混淆了。
$('.eventcontainer.button').click(function() {
$.post('javas.php', function(data) {
$(this).parent('div').find('.status').html(data);
})
});
点击$.post
来电后,this
不再等于您点击的项目。在调用post之前,您需要将该元素保存在局部变量中。
像
这样的东西$('.eventcontainer.button').click(function() {
var self = this;
$.post('javas.php', function(data) {
$(self).parent('div').find('.status').html(data);
})
});