【问题标题】:Checkbox toggle with two different properties具有两个不同属性的复选框切换
【发布时间】:2019-01-01 07:37:51
【问题描述】:

我有一个不同条目的列表,它们都有两个属性,用于对它们进行分类的属性:在我的示例中为 data-onedata-two

通过点击复选框,可以根据这些属性切换这些 div。

但是我遇到了一个问题:

  • 第一次单击复选框时,div 切换正常。
  • 当在第一步之后点击第二次导航的复选框时,由于命令是切换,所有在第一次点击时切换并具有第二次点击属性的 div 都会被切换回来。

这样没有多大意义,但here 是一个更易于理解的简单示例:

/*---- Content of entries ----*/

$(".entry").each(function() {

  var one = $(this).attr("data-one"),
    two = $(this).attr("data-two");

  $(this).html("My data-one is <span>" + one + "</span> and my data-two is <span>" + two + "</span>");
});


/*--- Fix button ---*/

$("button.fix").on("click", function() {
  $(".entry")
    .show()
    .end()
    .not(".shown")
    .addClass("shown");
  $("input[type=checkbox]").prop("checked", true);
});


/*--- Toggle entries ---*/

$(".button[data-one]").on("click", function() {
  var checkBoxes = $(this).children("input[type=checkbox]");
  checkBoxes.prop("checked", !checkBoxes.prop("checked"));

  var whichData = $(this).attr("data-one");

  $(".entry")
    .filter("[data-one='" + whichData + "']")
    .slideToggle("slow")
    .toggleClass("shown")
  /*
    .end()
    .filter("[data-one!='" + whichData + "']")
    .not(".shown")
    .hide()
  */
  ;
});

$(".button[data-two]").on("click", function() {
  var checkBoxes = $(this).children("input[type=checkbox]");
  checkBoxes.prop("checked", !checkBoxes.prop("checked"));

  var whichData = $(this).attr("data-two");

  $(".entry")
    .filter("[data-two='" + whichData + "']")
    .slideToggle("slow")
    .toggleClass("shown")
  /*
    .end()
    .filter("[data-two!='" + whichData + "']")
    .not(".shown")
    .hide()
  */
  ;
});
html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: white;
  font-family: Helvetica, sans-serif;
  font-size: 1em;
  text-align: center;
  color: black;
}

.nav {
  height: 180px;
  display: flex;
  justify-content: space-evenly;
  align-content: center;
}

.nav label {
  display: flex;
  margin: 10px;
  cursor: pointer;
}

.button {
  position: relative;
  float: left;
  display: block;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  background-color: #606062;
  background-image: linear-gradient(#474749, #606062);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.15), inset 0 -1px 1px rgba(0, 0, 0, 0.15);
  transition: all 0.15s ease;
}

.button svg {
  position: absolute;
  top: 3px;
  left: 3px;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke: #fff;
  stroke-width: 2;
  stroke-dasharray: 17;
  stroke-dashoffset: 17;
  transform: translate3d(0, 0, 0);
}

.button+span {
  float: left;
  margin-left: 6px;
}

.nav input[type="checkbox"] {
  position: absolute;
  opacity: 0;
}

.nav input[type="checkbox"]:checked+.button {
  background-color: #606062;
  background-image: linear-gradient(#255cd2, #1d52c1);
}

.nav input[type="checkbox"]:checked+.button svg {
  stroke-dashoffset: 0;
  transition: all 0.15s ease;
}

.container {
  margin-top: 5vh;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly;
  align-content: space-around;
}

.entry {
  width: 20vw;
  height: 20vh;
  border: 2px solid;
  margin: 5px;
}

.entry span {
  font-weight: 900;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main">

  <header>
    <div class="nav">

      <button class="fix">
    Fix (show everything)
    </button>
      <div class="navigation">

        <h2>Data-one</h2>

        <label for="ex1">
          <input type="checkbox" id="ex1" checked>
          <span class="button" data-one="1">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 1</span>
        </label>

        <label for="ex2">
          <input type="checkbox" id="ex2" checked>
          <span class="button" data-one="2">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 2</span>
        </label>

        <label for="ex3">
          <input type="checkbox" id="ex3" checked>
          <span class="button" data-one="3">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 3</span>
        </label>

        <label for="ex4">
          <input type="checkbox" id="ex4" checked>
          <span class="button" data-one="4">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 4</span>
        </label>

      </div>


      <div class="navigation">

        <h2>Data-two</h2>

        <label for="ex5">
          <input type="checkbox" id="ex5" checked>
          <span class="button" data-two="5">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 5</span>
        </label>

        <label for="ex6">
          <input type="checkbox" id="ex6" checked>
          <span class="button" data-two="6">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 6</span>
        </label>

        <label for="ex7">
          <input type="checkbox" id="ex7" checked>
          <span class="button" data-two="7">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 7</span>
        </label>

        <label for="ex8">
          <input type="checkbox" id="ex8" checked>
          <span class="button" data-two="8">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 8</span>
        </label>

      </div>


    </div>
  </header>


  <div class="container">
    <div class="entry shown" data-one="1" data-two="6">
    </div>
    <div class="entry shown" data-one="3" data-two="6">
    </div>
    <div class="entry shown" data-one="2" data-two="5">
    </div>
    <div class="entry shown" data-one="1" data-two="7">
    </div>
    <div class="entry shown" data-one="1" data-two="5">
    </div>
    <div class="entry shown" data-one="4" data-two="8">
    </div>
    <div class="entry shown" data-one="3" data-two="6">
    </div>
    <div class="entry shown" data-one="2" data-two="7">
    </div>
    <div class="entry shown" data-one="4" data-two="7">
    </div>
    <div class="entry shown" data-one="4" data-two="8">
    </div>
    <div class="entry shown" data-one="3" data-two="5">
    </div>
    <div class="entry shown" data-one="1" data-two="6">
    </div>
    <div class="entry shown" data-one="2" data-two="6">
    </div>
  </div>

</div>

有没有更好的方法,例如将这两个属性放在一个 dom 中,或者使用 isotope 之类的东西(即使我不想......)?

【问题讨论】:

  • 我注意到示例 7 和 8 的属性为“data-one”,尽管它们位于“data-two”组中。
  • @PoorlyWrittenCode 非常感谢您的提醒
  • @PoorlyWrittenCode 我编辑了那个。您是否知道如何解决该问题?

标签: jquery checkbox attributes toggle


【解决方案1】:

我已经编写了一些代码并将解释它是如何工作的:

/*---- Content of entries ----*/

$(".entry").each(function() {
  var one = $(this).attr("data-one"), two = $(this).attr("data-two");
  $(this).html("My data-one is <span>" + one + "</span> and my data-two is <span>" + two + "</span>");
});

/*--- Fix button ---*/

$("button.fix").on("click", function() {
  $(".entry").show().end().not(".shown").addClass("shown");
  $("input[type=checkbox]").prop("checked", true);
});

/*--- Toggle entries ---*/

$("label").on("click", function() {
  if ($(this).find(".button").attr("data-one")) {
    if ($(this).find('input[type=checkbox]').prop("checked") === true) {
      $(".entry").filter("[data-one='" + $(this).find(".button").attr("data-one") + "']").addClass("shown");
    } else {
      $(".entry").filter("[data-one='" + $(this).find(".button").attr("data-one") + "']").removeClass("shown");
    }
  } else {
    if ($(this).find('input[type=checkbox]').prop("checked") === true) {
      $(".entry").filter("[data-two='" + $(this).find(".button").attr("data-two") + "']").addClass("shown2");
    } else {
      $(".entry").filter("[data-two='" + $(this).find(".button").attr("data-two") + "']").removeClass("shown2");
    }
  }
});
html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: white;
  font-family: Helvetica, sans-serif;
  font-size: 1em;
  text-align: center;
  color: black;
}

.nav {
  height: 180px;
  display: flex;
  justify-content: space-evenly;
  align-content: center;
}

.nav label {
  display: flex;
  margin: 10px;
  cursor: pointer;
}

.button {
  position: relative;
  float: left;
  display: block;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  background-color: #606062;
  background-image: linear-gradient(#474749, #606062);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.15), inset 0 -1px 1px rgba(0, 0, 0, 0.15);
  transition: all 0.15s ease;
}

.button svg {
  position: absolute;
  top: 3px;
  left: 3px;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke: #fff;
  stroke-width: 2;
  stroke-dasharray: 17;
  stroke-dashoffset: 17;
  transform: translate3d(0, 0, 0);
}

.button+span {
  float: left;
  margin-left: 6px;
}

.nav input[type="checkbox"] {
  position: absolute;
  opacity: 0;
}

.nav input[type="checkbox"]:checked+.button {
  background-color: #606062;
  background-image: linear-gradient(#255cd2, #1d52c1);
}

.nav input[type="checkbox"]:checked+.button svg {
  stroke-dashoffset: 0;
  transition: all 0.15s ease;
}

.container {
  margin-top: 5vh;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly;
  align-content: space-around;
}

.entry {
  width: 20vw;
  height: 20vh;
  border: 2px solid;
  margin: 5px;
  display: none;
}

.entry.shown.shown2 {
  display: block;
}

.entry span {
  font-weight: 900;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main">

  <header>
    <div class="nav">

      <button class="fix">
    Fix (show everything)
    </button>
      <div class="navigation">

        <h2>Data-one</h2>

        <label for="ex1">
          <input type="checkbox" id="ex1" checked="true">
          <span class="button" data-one="1">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 1</span>
        </label>

        <label for="ex2">
          <input type="checkbox" id="ex2" checked="true">
          <span class="button" data-one="2">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 2</span>
        </label>

        <label for="ex3">
          <input type="checkbox" id="ex3" checked="true">
          <span class="button" data-one="3">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 3</span>
        </label>

        <label for="ex4">
          <input type="checkbox" id="ex4" checked="true">
          <span class="button" data-one="4">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 4</span>
        </label>

      </div>


      <div class="navigation">

        <h2>Data-two</h2>

        <label for="ex5">
          <input type="checkbox" id="ex5" checked="true">
          <span class="button" data-two="5">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 5</span>
        </label>

        <label for="ex6">
          <input type="checkbox" id="ex6" checked="true">
          <span class="button" data-two="6">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 6</span>
        </label>

        <label for="ex7">
          <input type="checkbox" id="ex7" checked="true">
          <span class="button" data-two="7">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 7</span>
        </label>

        <label for="ex8">
          <input type="checkbox" id="ex8" checked="true">
          <span class="button" data-two="8">
            <svg width="12px" height="11px" viewBox="0 0 12 11">
              <polyline points="1 6.29411765 4.5 10 11 1"></polyline>
            </svg>
          </span>
          <span>Example 8</span>
        </label>

      </div>


    </div>
  </header>


  <div class="container">
    <div class="entry shown shown2" data-one="1" data-two="6">
    </div>
    <div class="entry shown shown2" data-one="3" data-two="6">
    </div>
    <div class="entry shown shown2" data-one="2" data-two="5">
    </div>
    <div class="entry shown shown2" data-one="1" data-two="7">
    </div>
    <div class="entry shown shown2" data-one="1" data-two="5">
    </div>
    <div class="entry shown shown2" data-one="4" data-two="8">
    </div>
    <div class="entry shown shown2" data-one="3" data-two="6">
    </div>
    <div class="entry shown shown2" data-one="2" data-two="7">
    </div>
    <div class="entry shown shown2" data-one="4" data-two="7">
    </div>
    <div class="entry shown shown2" data-one="4" data-two="8">
    </div>
    <div class="entry shown shown2" data-one="3" data-two="5">
    </div>
    <div class="entry shown shown2" data-one="1" data-two="6">
    </div>
    <div class="entry shown shown2" data-one="2" data-two="6">
    </div>
  </div>

</div>

首先,您需要向.entry div 添加第二个类(我在下面的示例和sn-p 中添加了类shown2):

<div class="entry shown shown2" data-one="1" data-two="6"></div>

然后添加一些 CSS 以隐藏所有 .entry div,如果 .entry 同时具有类 .shown.shown2,则添加另一个规则以覆盖先前的规则:

.entry {
  ...
  display: none;
}

.entry.shown.shown2 {
  display: block;
}

最后,这个 Javascript 将替换您在 .button[data-one].button[data-two] 上的点击功能:

$("label").on("click", function() { //Gets click events on all <label> elements
  if ($(this).find(".button").attr("data-one")) { //If data-one
    if ($(this).find('input[type=checkbox]').prop("checked") === true) {
      $(".entry").filter("[data-one='" + $(this).find(".button").attr("data-one") + "']").addClass("shown");
    } else {
      $(".entry").filter("[data-one='" + $(this).find(".button").attr("data-one") + "']").removeClass("shown");
    }
  } else { //If data-two
    if ($(this).find('input[type=checkbox]').prop("checked") === true) {
      $(".entry").filter("[data-two='" + $(this).find(".button").attr("data-two") + "']").addClass("shown2");
    } else {
      $(".entry").filter("[data-two='" + $(this).find(".button").attr("data-two") + "']").removeClass("shown2");
    }
  }
});

不幸的是,这消除了幻灯片效果,但可以在 CSS 中进行某种程度的复制,如果你想要的话,我愿意在聊天室中引导你完成。

CodePen

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2018-04-25
    相关资源
    最近更新 更多