将复选框选中状态传递给AJAX脚本

时间:2017-07-21 07:34:44

标签: javascript jquery ajax checkbox

我在第一列中有一个带有复选框的表 - 当选中时,它执行一个AJAX脚本,用所选值更新PHP会话变量。这一切都运行良好,但我现在需要更新它,以便它传递复选框的状态,以便我可以将选定的值添加到PHP数组或从PHP数组中删除选定的值。

我无法弄清楚如何将已检查状态作为附加参数包含在AJAX post请求中 - 如果没有这个我无法确定是否从数组中添加或删除productID值。

这是我到目前为止所拥有的:

$(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();
    });
  });
});
<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>

2 个答案:

答案 0 :(得分:1)

实际上有多种解决方案。

您可以使用$("input.select-item").attr$("input.select-item").prop$("input.select-item").is进行此操作:

$("input.select-item").attr( "checked" ): checked
$("input.select-item").prop( "checked" ): true
$("input.select-item").is( ":checked" ): true

就个人而言,我更喜欢$().prop

这是一份文档http://api.jquery.com/prop/

但是要选中已选中的复选框元素,您只需使用:checked选择器:

$("select-item:checked")

答案 1 :(得分:0)

解决方案非常简单:

您可以使用以下代码获取值:$('#idOfElement').is(":checked");,返回truefalse。然后,您可以将该值附加到data JSON,并通过ajax发送。

追加示例:

data["checkboxChecked"] = $('#idOfElement').is(":checked");

希望这有帮助!