当前位置:首页 > 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 事件实时监听选项变化:

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 伪元素模拟视觉样式:

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>

js 实现 radio

标签: jsradio
分享给朋友:

相关文章

js实现轮播

js实现轮播

实现基础轮播效果 使用HTML结构创建轮播容器和图片元素: <div class="carousel"> <div class="carousel-inner">…

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.…

js实现

js实现

实现 JavaScript 功能的方法 在 JavaScript 中实现特定功能通常涉及多个步骤。以下是常见的实现方法: 基础语法和变量声明 使用 let 或 const 声明变量: let co…

js实现轮播

js实现轮播

实现轮播图的基本思路 轮播图的核心逻辑是通过定时切换展示的图片或内容,通常结合左右滑动按钮和指示器(小圆点)增强交互性。以下是基于原生JavaScript的实现方法。 HTML结构 构建轮播图的H…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取…

js实现类

js实现类

JavaScript 实现类的几种方式 JavaScript 是一种基于原型的语言,但在 ES6 之后引入了 class 语法糖,使得实现类更加直观。以下是几种常见的实现方式: 使用构造函数 传统的…