jquery - 如何设置一个单选按钮应该始终处于事件状态

标签 jquery html css radio-button css-animations

我有一个带有男性和女性的单选按钮。

我需要以下问题的帮助。

  1. 我必须让男性单选按钮始终处于事件状态。
  2. 如何增加尺寸?我试图提供一些填充,但在后台发现了问题。
  3. 如何在上面设置一些动画?

你愿意帮我吗?

$('label.option').click(function() {
  $('input[type=radio]').attr('checked',null);
  $('label.option').removeClass("checked");
  $(this).prev().attr('checked',"checked");
  $(this).addClass("checked");
})
.border {
  border:1px solid #000;
  width: 150px;
  border-radius: 30px;
  text-align: center;
  display: flex;
}

.width-half {
  width: 75px;
}

input[type=radio] {
  display: none;
}

.checked {
  background-color: #00a2ff;
  border-radius: 30px;
  display: block;
  color: #fff;
}
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<div class="border">
  <div class="width-half">
    <input type="radio" name="gender" value="male">
    <label class="option">Male</label>
  </div>
  <div class="width-half">
    <input type="radio" name="gender" value="female">
    <label class="option">Female</label>
  </div>
</div>

最佳答案

1。始终处于事件状态

只需将您的输入属性设置为选中

<input type="radio" name="gender" value="male" checked>

然后将与选中状态的输入相邻的同级标签作为目标,就像这样

input[type=radio]:checked + label {
    background-color: #00a2ff;
    border-radius: 30px;
    display: block;
    color: #fff;
}

2。尺寸

Label是行内级元素,直接设置为block,然后padding随意设置

label {
    display: block;
    padding: 5px;
}

3。动画

取决于您想为什么设置动画,但只需将过渡设置为全部即可。

label {
    transition: all 300ms ;
}

关于jquery - 如何设置一个单选按钮应该始终处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46934404/

相关文章:

html - 需要帮助找出 div 的边界

javascript - jquery if语句并删除iframe

javascript - 如何在表单提交上生成模态而不是页面重定向

css - 将子样式添加到父级

html - 关键帧动画无法正常工作 - CSS

php - 将 zip 文件上传到 PHP 上传网站不起作用,但适用于 mac

css - 使用 CSS 使列表中的背景图像高于背景颜色

jquery - 当使用 PhoneGap 在移动设备上安装应用程序时,famo.us Surface 中的 iosslider 无法工作

javascript - 动态 html 表单值未存储在数据库中

html - 固定 div 从父级获取宽度为 100%