我怎样才能选择正确的div

时间:2011-12-16 13:35:08

标签: javascript jquery html css ajax

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


}

?>

我需要确定单击按钮的div,这是ajax函数,我需要在php标签中回显num但是我只希望它在单击按钮的类/帖子中回显它是在一个while循环中,按钮有很多复制品。

<script language="JavaScript" type="text/javascript">
 var num = 1;

function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "javas.php";



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...";

}

然后,如何在单击按钮的特定Div中显示信息/数据。正如我现在所说的那样,回声发生在它上升的第一堂课中! 非常感谢,谢谢!

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你可以按下多个按钮,你想知道女巫被按下了。

将此添加到onclick事件

   function(){ whateverFunctionYouUsed(this)}

或者您可以在将其发送到您的功能之前获取所需的属性。

如果你有firefox + firebug,请使用它来查看对象的所有属性并获取所需内容:

  function(){ 
          console.log(this)
          whateverFunctionYouUsed(this)
  }

答案 1 :(得分:0)

使用jQuery很容易:

$("yourContainerId > div").click(function () {
  var index = $("div").index(this);
  alert(index);
});

http://api.jquery.com/index/