当前位置:首页 > 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实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现二叉树

js实现二叉树

二叉树的基本概念 二叉树是一种树形数据结构,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的常见操作包括插入、删除、遍历等。 二叉树的节点定义 在JavaScript中,二叉树的节点可…

js实现目录

js实现目录

实现目录的基本思路 在JavaScript中实现目录功能通常涉及以下几个核心步骤:解析文档中的标题元素(如h1-h6),动态生成目录结构,并添加交互逻辑(如点击跳转)。以下是具体实现方法: 解析标题…

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…

js实现正交

js实现正交

正交的概念 正交在数学和计算机科学中通常指两个向量垂直或线性无关。在编程中,正交性常被用于设计模块化、低耦合的系统。 向量正交判断 判断两个向量是否正交可以通过点积是否为0来实现: functio…

js实现视口

js实现视口

js实现视口检测的方法 使用JavaScript检测元素是否进入视口(viewport)可以通过Intersection Observer API或手动计算元素位置实现。以下是两种常见方法: Int…