【问题标题】:If checkbox "A" and "C" is checked make checkbox "B" checked to [closed]如果选中复选框“A”和“C”,则将复选框“B”选中为 [关闭]
【发布时间】:2020-01-09 20:37:12
【问题描述】:

您好,我有一个问题,不知道是否有一个很好的解决方案。

<input type="checkbox" name="a" value="a">a<br>
<input type="checkbox" name="b" value="b">b<br>
<input type="checkbox" name="c" value="c">c<br>
<input type="checkbox" name="d" value="c">d<br>
<input type="checkbox" name="e" value="c">e<br>

我喜欢让它更像是一个“范围”,所以如果用户点击“a”然后点击“d”,那么两者之间的所有内容都会被选中。

我可以对所有场景进行 if 语句,但也许有更好的方法来做到这一点?

谢谢

【问题讨论】:

  • 原生js?你有 jquery 会很容易
  • 你试过编码吗?或者你有什么想法吗?

标签: javascript html arrays checkbox


【解决方案1】:

使用 jQuery 非常简单 - 像这样使用 prevAll

$('input[type="checkbox"]').on("change", function() {
  if ($(this).is(":checked")) this.prevAll('input[type="checkbox"]').prop("checked", true);
});

【讨论】:

    猜你喜欢
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多