嘿伙计,所以我有一个填充了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'>
如果你能帮我一把,我会很感激!
大卫
答案 0 :(得分: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
}
});
});
答案 1 :(得分: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()
}
});
});
答案 2 :(得分:1)
$(this)
指的是点击的按钮,因此除非表格行是DOM中按钮的后代,否则$(this).find(".search-fade")
将无法找到它们。
您应该将$(this)
更改为表格行的父元素。
答案 3 :(得分: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 &lt; - 这需要淡出
----的 TD 强>
--------- input.cancelmember &lt; -click发生在这里