<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>
大家好,我想在服务器响应上执行最后一个 if 条件,这会改变 res 变量 问题是当我将整个 if 代码块放入 ajax success 中时,变量“this”值被更改,这会禁用代码更改 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>
提前感谢您的帮助...
最佳答案
我不太确定您想要实现什么目的(例如,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>
关于javascript - 根据服务器响应 vai php 更改 ajax 成功时的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29107775/