简体   繁体   English

如何获得复选框的最新检查值?

[英]How do i get the latest checked value of the checkbox?

I have the following code that would get the value of the checkbox, but not the one that is checked the latest, it get the last(?) value of the checkbox. 我有以下代码将获取复选框的值,但没有被检查到最新的代码,它将获取复选框的last(?)值。 I would like to get the value of the checkbox that is recently checked. 我想获取最近选中的复选框的值。

This is my html. 这是我的html。

<div class="custom-control custom-checkbox">
  <input
    type="checkbox"
    class="custom-control-input chkbx"
    value="123456"
    data-valuetwo="Mike"
    id="customCheck32"
    name="choice[]"
  />
  <label class="custom-control-label" for="customCheck32">Mike</label>
</div>

<div class="custom-control custom-checkbox">
  <input
    type="checkbox"
    class="custom-control-input chkbx"
    value="6542321"
    data-valuetwo="John"
    id="customCheck33"
    name="choice[]"
  />
  <label class="custom-control-label" for="customCheck33">John</label>
</div>

<div id="selecteditems" style="border: 1px solid black"></div>

<div class="itemhead"></div>

This is my script. 这是我的剧本。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
  $(".chkbx").click(function() {
    var selected = "";
    var latestchoice = "";
    $(".chkbx:checked").each(function() {
      selected += $(this).attr("data-valuetwo") + "<br>";
      latestchoice = $(this).attr("data-valuetwo");
    });
    $("#selecteditems").html(selected);

    if ($(".itemhead").is(":empty")) {
      $(".itemhead").html(latestchoice);
    } else {
      $(".itemhead").empty();
      $(".itemhead").html(latestchoice);
    }
  });
</script>

With the following code, if I check Mike first, then John, I could see the latest choice changing from Mike to John. 使用以下代码,如果我先检查Mike,然后再检查John,则可以看到最新的选择从Mike更改为John。 However, if I selected John first and select Mike, the value of latest choice would not change to Mike. 但是,如果我先选择John并选择Mike,则最新选择的值将不会更改为Mike。 Thank you! 谢谢!

To do that, you'll have to remember when the checkboxes were checked. 要做到这一点,你必须复选框被检查要记住。 One way to do that is to use jQuery's data cache for elements, see comments: 一种方法是对元素使用jQuery的数据缓存,请参见注释:

$(".chkbx").click(function() {
  var $this = $(this);
  // If this checkbox is checked...
  if (this.checked) {
    // ...remember when
    $this.data("checked", Date.now());
  } else {
    // Not checked, remove the data
    $this.removeData("checked");
  }
  var selected = "";
  var latestchoice = "";
  // Get and sort the checked checkboxes
  var checked = $(".chkbx:checked").get().sort(function(a, b) {
    return $(a).data("checked") - $(b).data("checked");
  });
  // Loop through them in sorted order
  checked.forEach(function(cb) {
    var $cb = $(cb);
    selected += $cb.attr("data-valuetwo") + "<br>";
    latestchoice = $cb.attr("data-valuetwo");
  });
  $("#selecteditems").html(selected);

  if ($(".itemhead").is(":empty")) {
    $(".itemhead").html(latestchoice);
  } else {
    $(".itemhead").empty();
    $(".itemhead").html(latestchoice);
  }
});

Live Example: 现场示例:

 $(".chkbx").click(function() { var $this = $(this); // If this checkbox is checked... if (this.checked) { // ...remember when $this.data("checked", Date.now()); } else { // Not checked, remove the data $this.removeData("checked"); } var selected = ""; var latestchoice = ""; // Get and sort the checked checkboxes var checked = $(".chkbx:checked").get().sort(function(a, b) { return $(a).data("checked") - $(b).data("checked"); }); // Loop through them in sorted order checked.forEach(function(cb) { var $cb = $(cb); selected += $cb.attr("data-valuetwo") + "<br>"; latestchoice = $cb.attr("data-valuetwo"); }); $("#selecteditems").html(selected); if ($(".itemhead").is(":empty")) { $(".itemhead").html(latestchoice); } else { $(".itemhead").empty(); $(".itemhead").html(latestchoice); } }); 
 <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input chkbx" value="123456" data-valuetwo="Mike" id="customCheck32" name="choice[]" /> <label class="custom-control-label" for="customCheck32">Mike</label> </div> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input chkbx" value="6542321" data-valuetwo="John" id="customCheck33" name="choice[]" /> <label class="custom-control-label" for="customCheck33">John</label> </div> <div id="selecteditems" style="border: 1px solid black"></div> <div class="itemhead"></div> This is my script. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> 

That said, a list you can drag items around in might make for a better UX. 也就是说,可以在其中拖移项目的列表可能会带来更好的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM