Jquery Tracking复选框已从选中更改为未选中或已取消选中

时间:2011-10-25 17:10:09

标签: javascript jquery checkbox

假设我有四个复选框,格式为Check-a,Check-b,Check-c,Check-d。

已经选中了Check-a和Check-b。我可以使用Jquery来检查或取消选中哪个复选框。

现在我取消选中了Check-a。所以Check-a被检查了,我已经检查了这个未经检查。所以现在Check-c和Check-d已经取消选中,Check-a没有被我检查。我正在寻找一种方法来跟踪哪些是未选中的,而且之前未选中。

有人可以告诉我一个方法吗?

2 个答案:

答案 0 :(得分:1)

如果您需要一种点击历史记录,为什么不使用额外的字段?

HTML:

<form>
    <div>
        <input type="checkbox" name="fruit" value="o" id="orange" />
        <label for="orange">orange</label>
    </div>
    <div>
        <input type="checkbox" name="fruit" value="a" id="apple" />
        <label for="apple">apple</label>
    </div>
    <div>
        <input type="text" name="history" id="history" />
    </div>
</form>

jquery脚本:

$("input").click(function() {
    $("#history").val( $("#history").val() + $(this).val() + ";");
});

你可以隐藏它并评估;之后分开的名单..

答案 1 :(得分:1)

假设与@definitelyundefinable相同的HTML,这确实有点相同,但它更清晰,因为它不使用隐藏字段和伪数组(作为隐藏字段中的字符串)

$(function() {
  var history = [];
  $("input").click(function() {
    history.push({
      id: this.id,
      checked: this.checked
    });
  });
  // If you'd like, you can initialize the array
  // with the initial values with the following line
  $("input").click();

});