javascript - 如果 AJAX 错误,请取消选中复选框

标签 javascript jquery ajax checkbox

我有一个表,第一列中有一个复选框 - 当选中时,它会执行一个 AJAX 脚本,用所选值更新 PHP session 变量。如果出现 AJAX 错误,我希望该复选框保持未选中状态 - 目前,当脚本出现 AJAX 错误时,该复选框将保持选中状态。

这是表格和脚本:

$(document).ready(function() {
  $("input.select-item").click(function() {
    var productID = $(this).val();
    // Create a reference to $(this) here:
    $this = $(this);
    $.post('productSelections.php', {
      type: 'updateSelections',
      productID: productID,
      selectionType: 'single'
    }, function(data) {
      data = JSON.parse(data);
      if (data.error) {
        var ajaxError = (data.text);
        var errorAlert = 'There was an error updating the Product Selections';
        $this.closest('td').addClass("has-error");
        $("#updateSelectionsErrorMessage").html(errorAlert);
        $("#updateSelectionsError").show();
        return; // stop executing this function any further
      } else {
        $this.closest('td').addClass("success")
        $this.closest('td').removeClass("danger");
      }
    }).fail(function(xhr) {
      var httpStatus = (xhr.status);
      var ajaxError = 'There was an error updating the Product Selections';
      $this.closest('td').addClass("danger");
      $("#updateSelectionsErrorMessage").html(ajaxError);
      $("#updateSelectionsError").show();
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<table class="table table-condensed table-striped table-bordered">
  <thead>
    <th><input type="checkbox" class="select-all checkbox" name="select-all" /></th>
    <th class="text-center" scope="col">Product ID</th>
    <th class="text-center" scope="col">Description</th>
  </thead>
  <tbody>

    <tr class="" id="85799">
      <td id="AT36288"><input type="checkbox" class="select-item checkbox" name="select-item" value="AT36288" /></td>
      <td>AT36288</td>
      <td>Apples</td>
      <td></td>
    </tr>
    <tr class="" id="85800">
      <td id="AT36289"><input type="checkbox" class="select-item checkbox" name="select-item" value="AT36289" /></td>
      <td>AT36289</td>
      <td>Bananas</td>
      <td></td>
    </tr>
    <tr class="" id="85801">
      <td id="AT36290"><input type="checkbox" class="select-item checkbox" name="select-item" value="AT36290" /></td>
      <td>AT36290</td>
      <td>Oranges</td>
      <td></td>
    </tr>
    <tr class="" id="85803">
      <td id="AT36292"><input type="checkbox" class="select-item checkbox" name="select-item" value="AT36292" /></td>
      <td>AT36292</td>
      <td>Grapes</td>
      <td></td>
    </tr>
  </tbody>
</table>

我正在添加一个错误类以将其着色为红色并显示警报,但希望尽可能防止该复选框保持选中状态。

最佳答案

您可以尝试ajax失败 block 中的任何一个

$this.attr('checked', false);
$this.prop('checked', false);
$this.removeAttr('checked');

关于javascript - 如果 AJAX 错误,请取消选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45230701/

相关文章:

javascript - jQuery 数据表 : Uncaught TypeError: Cannot read property 'length' of undefined

javascript - react : Invoke redirect to page, 并传递参数

jquery - 字符串的长度超过了 maxJsonLength 属性上设置的值

javascript - AJAX 获取请求在 iOS Safari 上有时会失败

javascript - 'drag' 的 highcharts 包装函数

javascript - 如何在 JavaScript 中创建动态数组?

javascript - 删除没有类的 jquery 元素

javascript - 离开页面时如何使用Jquery显示弹出调查

javascript - 如何使用ajax将从jquery for循环创建的输入值发送到php

javascript - jQuery Ajax 数据显示双重使用追加