<table class="table table-bordered table-striped table-hover datatable">
<thead>
<tr>
<th>ID</th>
<th>organisation Name</th>
<th>User name</th>
<th>Email</th>
<th>Contact No</th>
<th>IP</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr class="success"> <td>3</td>
<td>Harshit </td>
<td>AtulSaini</td>
<td>arpitkumar@gmail.com</td>
<td>786048</td>
<td>::1</td>
<td>14/03/2015</td><td><button id="status"><span class="label label-success">Active</span></button></td></tr><tr class="none"> <td>4</td>
<td>Meghaa.co.edu</td>
<td>megha</td>
<td>meghaa16@gmail.com</td>
<td>786048</td>
<td>::1</td>
<td>14/03/2015</td><td><button id="status"><span class="label label-success">Active</span></button></td></tr>
</tbody>
</table>
如果要在服务器响应上执行条件以改变res变量,那么我想要的最后一个 问题是当我把整个if代码块放在ajax成功然后变量&#34;这个&#34;值得到改变,禁用代码来改变tr 如果我把它保持在外面,就像我在这里所做的那样,变量res在成功函数中被破坏
<script type="text/javascript">
$(document).ready(function() {
$(".label").click(function() {
var idx = $(this).parents("tr").find('td:first').html();
var status = $(this).parents("tr").find('span').html();
var cname=$(this).parents('tr').attr("class");
alert(this);
$.ajax({
url : "scripts/update.php",
type : 'GET',
data : {
"status" : status,
"id" : idx
},
error : function(xhr, status, error) {
alert(error);
},
success : function(entry) {
var res = entry;
}
});
alert(status);
if (status == 'Active') {
$(this).parents("tr").removeClass(cname);
$(this).parents("tr").addClass("warning");
$(this).removeClass("label-success");
$(this).addClass("label-warning");
$(this).text("Deactive");
alert("Account Deacivated");
} else {
$(this).parents('tr').removeClass(cname);
$(this).parents('tr').addClass("success");
$(this).removeClass("label-warning");
$(this).addClass("label-success");
$(this).text("Active");
alert("Account Acivated");
}
});
});
</script>
Thanx提前帮助你......
答案 0 :(得分:0)
我不确定您要实现的目标(例如,res
/ entry
中存储了哪种数据)。但也许以下内容对您有所帮助:
<script type="text/javascript">
$(document).ready(function() {
$(".label").click(function() {
// store reference for later use (and to avoid creating the same object multiple times)
var item = $(this);
// use the previously stored jQuery item instead of recreating it every time with $(...)
var idx = item.parents("tr").find('td:first').html();
var status = item.parents("tr").find('span').html();
// ...
$.ajax({
url: "scripts/update.php",
type: 'GET',
data: {
"status": status,
"id": idx
},
error: function(xhr, status, error) {
alert(error);
},
success: function(entry) {
// `item` will still reference to the label you clicked on
}
});
});
});
</script>
答案 1 :(得分:0)
试试这个:
...
var $this = $(this);
$.ajax({
url : "scripts/update.php",
type : 'GET',
data : {
"status" : status,
"id" : idx
},
error : function(xhr, status, error) {
alert(error);
},
success : function(entry) {
changeStatuts(entry, $this);
}
});
var changeStatuts = function(status, ele){
if (status == 'Active') {
ele.parents("tr").removeClass(cname);
ele.parents("tr").addClass("warning");
ele.removeClass("label-success");
ele.addClass("label-warning");
ele.text("Deactive");
alert("Account Deacivated");
} else {
ele.parents('tr').removeClass(cname);
ele.parents('tr').addClass("success");
ele.removeClass("label-warning");
ele.addClass("label-success");
ele.text("Active");
alert("Account Acivated");
}
};
答案 2 :(得分:0)
好的,在阅读完评论之后,或许类似以下内容可以解决您的问题。但是:您的PHP代码似乎容易受到sql injection
的攻击<script type="text/javascript">
$(document).ready(function() {
$(".label").click(function() {
// store reference for later use (and to avoid creating the same object multiple times)
var item = $(this);
// use the previously stored jQuery item instead of recreating it every time with $(...)
var parentRow = item.parents("tr");
var idx = parentRow.find('td:first').html();
var status = parentRow.find('span').html();
$.ajax({
url: "scripts/update.php",
type: 'GET',
data: {
"status": status,
"id": idx
},
error: function(xhr, status, error) {
alert(error);
},
success: function(status) {
if (status == 'Active') {
parentRow.removeClass(cname);
parentRow.addClass("warning");
item.removeClass("label-success").addClass("label-warning").text("Deactive");
alert("Account Deacivated");
} else {
parentRow.removeClass(cname);
parentRow.addClass("success");
item.removeClass("label-warning").addClass("label-success").text("Active");
alert("Account Acivated");
}
}
});
});
});
</script>