【问题标题】:In Vue3, how to check value of each radio button checked from a single group of radio buttons?在 Vue3 中,如何检查从一组单选按钮中检查的每个单选按钮的值?
【发布时间】:2022-07-20 21:55:48
【问题描述】:

我刚刚开始使用 Vue3 进行培训,并且非常喜欢它。但是我遇到了一些使用 vanilla JS 似乎更容易(直观)的问题。

我有这组具有相同name 值的单选按钮

<template>
  <div class="container">
    <div class="inputs">
      <div class="input">
        <input
          id="01-01"
          v-model="voted"
          type="radio"
          name="VP-01"
          @change="showCheckedValue"
        />
        <label for="01-01">1</label>
      </div>

      <div class="input">
        <input
          id="01-02"
          v-model="voted"
          type="radio"
          name="VP-01"
          @change="showCheckedValue"
        />
        <label for="01-02">2</label>
      </div>
      <div class="input">
        <input
          id="01-03"
          v-model="voted"
          type="radio"
          name="VP-01"
          @change="showCheckedValue"
        />
        <label for="01-03">3</label>
      </div>

      <div class="input">
        <input
          id="01-04"
          v-model="voted"
          type="radio"
          name="VP-01"
          @change="showCheckedValue"
        />
        <label for="01-04">4</label>
      </div>
      <div class="input">
        <input
          id="01-05"
          v-model="voted"
          type="radio"
          name="VP-01"
          @change="showCheckedValue"
        />
        <label for="01-05">5</label>
      </div>
    </div>

    <div class="checked">
      <div class="one">Checked value of 1 : {{ voted }}</div>
      <div class="one">Checked value of 2 : {{ voted }}</div>
      <div class="one">Checked value of 3 : {{ voted }}</div>
      <div class="one">Checked value of 4 : {{ voted }}</div>
      <div class="one">Checked value of 5 : {{ voted }}</div>
    </div>
    <div class="voted">
      {{ voted }}
    </div>
  </div>
</template>


我可以使用 v-model 输出值,这很好...我还可以获取 CSS 样式的检查值...所以很好..

但是当检查一个输入字段(单选按钮)时,我会假设该按钮的ON 值定义了状态(也许我在这个假设中是错误的)......但是如果我点击我的代码它转换为ON 的任何按钮,但其他按钮也保持为ON.. 我假设如果选中该按钮,那么它将是ON,如果没有,那么它将是......也许null ?或false?我不确定...

我添加了一个名为 @change 的 show showCheckedValue 函数,但不知道如何使它工作...

这是我要测试的脚本和样式设置...我什至尝试将v-model= "voted" 更改为每个按钮都有一个不同的按钮,例如v-model="voted1"v-model="voted2" 等等.. 但这没有用要么..

所以问题来了 如何输出或复制一组单选按钮的选中值?如果选中其中一个,则应为trueon,其他应为falsenull

这是我在 codepen 上测试的链接。https://codepen.io/alimbolar/pen/OJvmNQB


<script setup>
import { ref } from "vue";

const voted = ref(null);
// const voted2 = ref(null);
// const voted3 = ref(null);
// const voted4 = ref(null);
// const voted5 = ref(null);
</script>

<!-- Use preprocessors via the lang attribute! e.g. <style lang="scss"> -->
<style>
html {
  box-sizing: border-box;
}

#app {
  border: 1px solid red;
  margin: 50px;
  display: grid;
  place-items: center;
  height: 100vh;
}
.container {
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  width: 100%;
  height: 100%;
}

.inputs {
  border: 1px solid green;
  padding: 20px;
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}

.checked {
  border: 1px solid red;
  width: 100%;
  display: flex;
}

.checked > * {
  text-align: center;
  font-size: 0.8rem;
  padding: 1rem;
}

.voted {
  text-align: center;
  text-transform: uppercase;
  border: 1px solid orange;
  padding: 10px;
}

input:checked + label {
  color: red;
}
</style>

【问题讨论】:

    标签: javascript radio-button vuejs3


    【解决方案1】:

    您需要为每个radio button 提供一个value,以便voted ref 可以与之绑定。

    Please check the codepen

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 2012-07-23
      • 2013-09-04
      • 2015-06-19
      相关资源
      最近更新 更多