当前位置:首页 > JavaScript

js 实现 radio

2026-01-31 18:07:35JavaScript

实现单选按钮(Radio)的 JavaScript 方法

纯 HTML 实现基础 Radio 通过 HTML 原生标签即可创建单选按钮组,确保同一组的 name 属性相同以实现互斥选择:

<input type="radio" name="gender" id="male" value="male">
<label for="male">Male</label>

<input type="radio" name="gender" id="female" value="female">
<label for="female">Female</label>

动态获取选中值 使用 querySelector 结合 :checked 伪类获取当前选中的值:

const selectedValue = document.querySelector('input[name="gender"]:checked').value;
console.log(selectedValue); // 输出: male 或 female

事件监听实现 通过 change 事件实时监听选项变化:

js 实现 radio

const radios = document.querySelectorAll('input[name="gender"]');
radios.forEach(radio => {
  radio.addEventListener('change', (e) => {
    if (e.target.checked) {
      console.log('Selected:', e.target.value);
    }
  });
});

动态创建 Radio 按钮 通过 JavaScript 动态生成单选按钮并插入 DOM:

const container = document.getElementById('radio-container');
const options = ['Red', 'Green', 'Blue'];

options.forEach((color, index) => {
  const radio = document.createElement('input');
  radio.type = 'radio';
  radio.id = `color-${index}`;
  radio.name = 'color';
  radio.value = color.toLowerCase();

  const label = document.createElement('label');
  label.htmlFor = `color-${index}`;
  label.textContent = color;

  container.appendChild(radio);
  container.appendChild(label);
  container.appendChild(document.createElement('br'));
});

自定义样式 Radio 通过隐藏原生按钮并用 CSS 伪元素模拟视觉样式:

js 实现 radio

input[type="radio"] {
  opacity: 0;
  position: absolute;
}

input[type="radio"] + label::before {
  content: "";
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid #ddd;
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: middle;
}

input[type="radio"]:checked + label::before {
  background-color: #4CAF50;
  border-color: #4CAF50;
}

表单提交处理 在表单提交时验证 Radio 选择:

document.getElementById('myForm').addEventListener('submit', (e) => {
  const selected = document.querySelector('input[name="color"]:checked');
  if (!selected) {
    e.preventDefault();
    alert('Please select an option!');
  }
});

框架实现(Vue 示例) 使用 Vue 的数据绑定简化 Radio 管理:

<div id="app">
  <template v-for="option in options">
    <input 
      type="radio" 
      :id="option.value" 
      :value="option.value" 
      v-model="selected">
    <label :for="option.value">{{ option.text }}</label>
  </template>
  <p>Selected: {{ selected }}</p>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    selected: '',
    options: [
      { text: 'Apple', value: 'fruit_apple' },
      { text: 'Banana', value: 'fruit_banana' }
    ]
  }
});
</script>

标签: jsradio
分享给朋友:

相关文章

js 实现倒计时

js 实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是两种常见实现方式。 使用 setInterval 实…

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前年…

js 实现链表

js 实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,插入和删除操作效率较高。 链表的实现 在 JavaScrip…

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…

js实现祖玛

js实现祖玛

实现祖玛游戏的核心思路 祖玛游戏的核心玩法是发射彩色珠子,形成三个或以上相同颜色的珠子即可消除。以下是使用JavaScript实现的基本框架。 游戏初始化 创建画布并初始化游戏状态: const…

利用js实现

利用js实现

使用 JavaScript 实现 在 JavaScript 中,可以通过多种方式实现功能,具体取决于需求。以下是几种常见的方法: 方法一:使用原生 JavaScript // 示例代码 funct…