简体   繁体   English

我怎样才能 select 随机数量的复选框?

[英]How can I select a random amount of checkboxes?

    $('#select_all').on('click', function () {
        if (this.checked) {
            $('.check').each(function () {
                this.checked = true;
            });
        } else {
            $('.check').each(function () {
                this.checked = false;
            });
        }
    });
    $('.check').on('click', function () {

        if ($('.check:checked').length == $('.check').length) {
            $('#select_all').prop('checked', true);
        } else {
            $('#select_all').prop('checked', false);
        }
    });

Need to alter this above Select All code to only select some random amount of check boxes, like below code example.需要将上面的 Select 所有代码更改为只有 select 一些随机数量的复选框,如下面的代码示例。 Any help for this?有什么帮助吗?

$(".random-pick").click(function() {
  var maxAllowed = 6;
  // Count checkboxes
  var random_checkboxes = $("#content_subscribtion_ input.checkbox").size();
  // Check random checkboxes until "maxAllowed" limit reached
  while ($("#content_subscribtion_ input.checkbox:checked").size() < maxAllowed) {
    // Pick random checkbox
    var random_checkbox = Math.floor(Math.random() * random_checkboxes) + 1;
    // Check it
    $("#content_subscribtion_ input.checkbox:nth-child(" + random_checkbox + ")").prop("checked", true);
  }

  return false;
});

Using this tidy little shuffle method , we can create a random array of checkbox indexes, then slice it to a random number.使用这种整洁的小洗牌方法,我们可以创建一个随机的复选框索引数组,然后将其切片为随机数。 We can then iterate and use jQuery's get() to find the checkbox index in the randomized array to check.然后我们可以迭代并使用 jQuery 的get()在随机数组中找到要检查的复选框索引。

 Array.prototype.shuffle = function() { let m = this.length, i; while (m) { i = (Math.random() * m--) >>> 0; [this[m], this[i]] = [this[i], this[m]] } return this; } $('#select_all').on('click', function() { if ($(this).prop('checked')) { let minnum = 3, maxnum = 6 let rand = Math.min(maxnum, Math.floor(Math.random() * ($('.check').length - 1 - minnum)) + minnum) //create our keys array let keyArray = [...Array($('.check').length).keys()].shuffle().slice(0, rand) keyArray.forEach((chk_i, i) => { if (i < rand) $($('.check').get(chk_i)).prop('checked', true) }) } else { $('.check').prop('checked', false); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label>Select all <input type='checkbox' id="select_all"></label> <div class='cb'> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> </div>

I have 8 checkboxes on the UI and I am selecting 3 checkboxes randomly.我在 UI 上有 8 个复选框,我随机选择了 3 个复选框。 After each run, different checkboxes will be selected.每次运行后,将选中不同的复选框。 Please use the exact/clean/proper checkbox common unique identifier in the cy.get('xxxxxx') and use following code in JS:请在cy.get('xxxxxx')中使用 exact/clean/proper checkbox common unique identifier 并在 JS 中使用以下代码:

cy.get('section[class="table-body-cell checkbox-cell grid-1"] input')
  .then(
    ($items) => {
      return Cypress._.sampleSize($items.toArray(), 3)
    })
  .check({force: true});

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

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