简体   繁体   English

如何确定单击哪个按钮的div /类

[英]How to determine which div/class a button has been clicked in

This is my ajax post 这是我的ajax帖子

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

}
</script>

Now I need to determine whitch class the button was clicked in (in php) 现在,我需要确定单击按钮的鞭子类别(在php中)

<?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>";
  }
?>
</span>
</div>

When the button is clicked, the ajax function is called, however the function is being displayed in the first status div rather than in the div/class the button was clicked in. 单击按钮时,将调用ajax函数,但是该函数显示在第一个状态div中,而不是在单击按钮的div / class中。

instead of 代替

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

use 采用

  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(this);'>
               <div id = 'status'>lol</div>";
        echo "<br>";

and in javascript you can easily get which one was clicked by using the whole object after receiving it you can use data- attributes or use the index 并且在javascript中,您可以在接收到对象后通过使用整个对象轻松获得被点击的对象,您可以使用数据属性或索引

Use jQuery to find out which button has been clicked. 使用jQuery找出已单击的按钮。 Add a name attribute to your button. 将名称属性添加到您的按钮。

    $().function({
      $("some_button").click() {
      clicked_name = $(this).attr("name");
      }
    });

您可以使用一些相关数据更新隐藏字段,并在发布后进行检查。

At first, you need to realize that "id" attribute of the tag is supposed to be unique. 首先,您需要认识到标签的“ id”属性应该是唯一的。 So the correct approach to implement this would be to differentiate the IDs for exemple appending a unique number to them. 因此,实现此目的的正确方法是区分ID,例如在ID后面附加一个唯一的数字。 Kinda like this: 有点像这样:

$i = 0;
while ($row1 = mysql_fetch_array($event1))
{
   $i++;

   ...

   echo " ... <input type='submit' name='lol' value='dec$i' />
       <div id='status$i'>...</div>";
}

You can then check for the number you get after the command (I know this is crude) 然后,您可以检查命令后得到的数字(我知道这很粗糙)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM