dongwei1234 2011-12-14 21:06
浏览 40
已采纳

在php中找到正确的div / class的Ajax

<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>

I get an error when trying to run this code. What i am trying to do is try to read which div/class the button is being clicked in, then perform the relative ajax/jquery functionality in that class only.

This is the 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...";

}
  • 写回答

1条回答 默认 最新

  • dtdsbakn210537 2011-12-14 21:10
    关注

    As the comments have noted, you should not have jQuery in your PHP tags. Add some script script tags to your page's head, add a document.ready handler, then put the script in there.

    Just note that once you accomplish this, you'll still have some problems since you're getting your this values mixed up.

    $('.eventcontainer.button').click(function() { 
       $.post('javas.php', function(data) {
          $(this).parent('div').find('.status').html(data); 
       })
    });
    

    Once you hit the callback of your $.post call, this no longer is equal to the item you clicked on. You'll need to save that element in a local variable before the call to post.

    Something like

    $('.eventcontainer.button').click(function() { 
        var self = this;
        $.post('javas.php', function(data) {
          $(self).parent('div').find('.status').html(data);
       })
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集