1

嘿伙计们,我有一个由 php 填充的列表,这是布局:

<table>
     <tr>
        <td>
        ....
        <td>

    </tr>

    <tr class='firsttr' style='background:gray;'>
     <td>Remove selected</td>
     <td>First Name </td>
     <td>Last Name </td>
     <td>Email </td>
     <td>Phone </td>
     <td>Username </td>
     <td>Password </td>
     <td>Status </td>
     <td>Status Change Date</td>
     <td>#</td>
    </tr>


       <tr class='search-fade'> <!-- this is the part that I need to fade out based on which button was clicked for that user. -->
              <td>
        <input type='button' value='Remove employee'  name='cancelmember' class='cancelmember' />
      </td>
        //....table data    
               <td>
               $csvusername
    <input type='hidden' value='$csvusername'  name='username' class='removeemp-user'/>
    </td>                           
          </tr>                         
</table>

这是我的jQuery:

$(".cancelmember").click(function () {
        remove_name = $(this).parent().parent().find(".removeemp-user");


        $.ajax({
        type: "POST",
        dataType: "json",
        url: '/pcg/removemem.php',
        data: {
            'username': remove_name.val()
        },
        success: function(){
            alert("Employee Removed"),
            $(this).find(".search-fade").fadeOut()
        }
      });
    });

上面的代码效果很好我只是不知道如何淡出<tr class='search-fade'> 如果你能帮我一把,我将不胜感激!

大卫

4

4 回答 4

3

您需要缓存元素,以便可以在成功函数中使用它。您需要向上而不是向下遍历 dom 树才能找到表格行,因为按钮是 tr 的后代

$(".cancelmember").click(function () {
    remove_name = $(this).parent().parent().find(".removeemp-user");
    var $this = $(this); // <-- cache the current element

    $.ajax({
        type: "POST",
        dataType: "json",
        url: '/pcg/removemem.php',
        data: {
            'username': remove_name.val()
        },
        success: function () {
            alert("Employee Removed"),
            $this.closest(".search-fade").fadeOut(); // <-- use closest since button is descendant of table row
        }
    });
});
于 2013-02-15T17:16:00.887 回答
1

尝试这个,

$(".cancelmember").click(function () {
        remove_name = $(this).parent().parent().find(".removeemp-user");
        var $this = $(this); //Make sure this is parent element not button. You may have to adjust the element here

        $.ajax({
        type: "POST",
        dataType: "json",
        url: '/pcg/removemem.php',
        data: {
            'username': remove_name.val()
        },
        success: function(){
            alert("Employee Removed"),
            $this.find(".search-fade").fadeOut()
        }
      });
    });
于 2013-02-15T17:04:12.863 回答
1

$(this)指的是被点击的按钮,所以除非表格行是 DOM 中按钮的后代,$(this).find(".search-fade")否则不会找到它们。

您应该更改$(this)为表格行的父元素。

于 2013-02-15T17:04:43.887 回答
1
$(".cancelmember").click(function () {
        remove_name = $(this).parent().parent().find(".removeemp-user");
        var thisEL = $(this).parent().parent();

        $.ajax({
        type: "POST",
        dataType: "json",
        url: '/pcg/removemem.php',
        data: {
            'username': remove_name.val()
        },
        success: function(){
            alert("Employee Removed"),
            thisEL.fadeOut();
        }
      });
    });

你需要升2级。
tr.search-fade <- 这需要淡出
---- td
--------- input.cancelmember <-click 发生在这里

于 2013-02-15T17:15:09.580 回答