简体   繁体   English

如何通过单击其 div select 单选按钮?

[英]How to select radio button by clicking its div?

I have created 5 categories of radio buttons, each with 3 choices to choose from.我创建了 5 类单选按钮,每类都有 3 个选项可供选择。 I have inserted these radio buttons into its respective divs.我已将这些单选按钮插入到其各自的 div 中。 While building the radios buttons, I had believed that when the user clicks on the div itself, the radio button will be selected as well.在构建单选按钮时,我相信当用户单击 div 本身时,单选按钮也会被选中。 I found out that this is not the case.我发现事实并非如此。 At first I set input [type=radio] opacity 0 so the circle box is hidden, dont want it to show.起初我将 input [type=radio] opacity 0 设置为隐藏圆形框,不希望它显示。 I removed this css property to see if its being selected but its not.我删除了这个 css 属性,看看它是否被选中,但不是。 Is there a way to select the radio button without clicking on the circle, and by clicking on the div that it is in?有没有办法 select 单选按钮不点击圆圈,点击它所在的 div? Im think that maybe my HTMl is incorrect, not sure where to go with this.我认为也许我的 HTMl 不正确,不知道 go 在哪里。 Any tips are greatly appreciated.非常感谢任何提示。 I will post html of one category of the radio buttons and current display of the radio buttons.我将发布一类单选按钮的 html 和单选按钮的当前显示。

<main class="subscription__container">
      <section
        id="preferences"
        class="subscription__container--preferences box"
      >
        <div class="question__container">
          <h3 class="question__container--title">
            How do you drink your coffee?
          </h3>
          <img
            class="question__container--img"
            src="../assets/plan/desktop/icon-arrow.svg"
            alt="arrow"
          />
        </div>
        <div class="options__container">
          <div class="options__container--option">
            <input
              id="capsule"
              type="radio"
              data-preference="Capsule"
              value="Capsule"
              name="preferences"
              checked
            />
            <label for="capsule"></label>
            <h4 class="options__container--title">Capsule</h4>
            <p class="options__container--description">
              Compatible with Nespresso systems and similar brewers.
            </p>
          </div>
          <div class="options__container--option">
            <input
              id="filter"
              type="radio"
              data-preference="Filter"
              value="Filter"
              name="preferences"
            />
            <label for="filter"></label>
            <h4 class="options__container--title">Filter</h4>
            <p class="options__container--description">
              For pour over or drip methods like Aeropress, Chemex, and V60.
            </p>
          </div>
          <div class="options__container--option">
            <input
              id="espresso"
              type="radio"
              data-preference="Espresso"
              value="Espresso"
              name="preferences"
            />
            <label for="espresso"></label>
            <h4 class="options__container--title">Espresso</h4>
            <p class="options__container--description">
              Dense and finely ground beans for an intense, flavorful
              experience.
            </p>
          </div>
        </div>
      </section>
</main>

当前的外观,有没有办法通过单击其 div 和单选圆圈来选择单选按钮以显示无?

Your labels are not surrounding the div contents.您的标签没有围绕div内容。 They are currently just empty labels (eg, <label for="capsule"></label> ), so obviously nothing is happening.它们目前只是空标签(例如, <label for="capsule"></label> ),所以显然没有发生任何事情。

This should work:这应该有效:

 <main class="subscription__container"> <section id="preferences" class="subscription__container--preferences box" > <div class="question__container"> <h3 class="question__container--title"> How do you drink your coffee? </h3> <img class="question__container--img" src="../assets/plan/desktop/icon-arrow.svg" alt="arrow" /> </div> <div class="options__container"> <div class="options__container--option"> <input id="capsule" type="radio" data-preference="Capsule" value="Capsule" name="preferences" checked /> <label for="capsule"> <h4 class="options__container--title">Capsule</h4> <p class="options__container--description"> Compatible with Nespresso systems and similar brewers. </p></label> </div> <div class="options__container--option"> <input id="filter" type="radio" data-preference="Filter" value="Filter" name="preferences" /> <label for="filter"> <h4 class="options__container--title">Filter</h4> <p class="options__container--description"> For pour over or drip methods like Aeropress, Chemex, and V60. </p></label> </div> <div class="options__container--option"> <input id="espresso" type="radio" data-preference="Espresso" value="Espresso" name="preferences" /> <label for="espresso"> <h4 class="options__container--title">Espresso</h4> <p class="options__container--description"> Dense and finely ground beans for an intense, flavorful experience. </p></label> </div> </div> </section> </main>

put input radio inside label:将输入收音机放入 label 内:

<label class="options__container--option" for="espresso">
    <input id="espresso"
           type="radio"
           data-preference="Espresso"
           value="Espresso"
           name="preferences"/>
        <div>
            <h4 class="options__container--title">Espresso</h4>
            <p class="options__container--description">
              Dense and finely ground beans for an intense, flavorful
              experience.
            </p>
        </div>
</label>

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

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