AJAX调用传递<div>而不是<button>

时间:2018-04-05 23:14:22

标签: javascript php jquery ajax call

我想知道为什么这个ajax调用,传递div id而不是我想要的按钮。

            <?php
           while($info = mysqli_fetch_array($sql))
           {

            echo "<tr>

             <th>" .$info['name']. "</th>
             <th>" .$info['pass']. "</th>
             <th><a href=\"http://" .$info['link']. "\">" .$info['link']. "</a></th>
              <th><div class=\"delbuttons\" id='5'> <button  data-toggle=\"modal\" data-target=\"#myModal\"  id='" .$info['id'].  "'> Delete </button> </div> </th> </tr> ";

           }
           ?>
     </table>
     <script>
        $(document).ready(function(){
            $('.delbuttons').click(function() {
                $.ajax({
                type: 'get',
                url: 'ajax.php',
                data: { 
                    row: this.id
                },
                success: function(msg){
                    if (msg)
                        alert(msg);

                    location.reload();
                }
                });
            });
        });
     </script>

我将按钮ID设置为5只是为了调试,正如我所料,它返回“5”而不是按钮的id。

我试过了两个,得到&amp;交。

提前致谢!

1 个答案:

答案 0 :(得分:2)

您选择的是$('.delbuttons'),因此this是指<div>

请改为选择$('.delbuttons button'),或使用$(this).children(":first").attr("id") or some other way to get the first element child in jQuery代替this.id