javascript - 根据服务器响应 vai php 更改 ajax 成功时的变量值

标签 javascript php jquery html ajax

<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/

相关文章:

php - auth()->user()->id 在 Controller 中使用到 api.php 的路由时无法正常工作

javascript - Raphael JS - 禁用绘图中的 HREF

javascript - 为什么 text/plain 下载图像类型

JavaScript - 在不使用 CSS 文件和样式标签的情况下获取 HTML 文件中元素的大小?

javascript - 将图像 div 分成像素行并附加事件

php - 通过 exec() 从 php 运行服务器端 js

java - 需要从java返回一个json列表以使用json填充html中的下拉列表

JavaScript:如何将单个全局对象传递给一组类?

javascript - 我可以为 CSS 背景图像设置不透明度动画吗?

php - 字符串大小会影响MySQL中的选择查询速度吗?