javascript - 选中单选按钮(输入)时如何触发事件?

标签 javascript jquery html css

我有两个 type="radio" 输入,默认选中第一个选项。我想要实现的是在选中第二个选项(输入)时触发特定事件。我试图通过基本的“if/else”语句(检测到特定输入具有“checked”属性)来做到这一点,但它不起作用...

这是 HTML 部分:

<input type="radio" name="radio-group" id="sendnow" value="Send Now" checked>
<label for="sendnow" class="camp_lbl">Send Now</label>
<input type="radio" name="radio-group" id="sendlater" value="Schedule Send">
<label id="zzz" for="sendlater" class="camp_lbl">Schedule Send</label>

还有 CSS:

#send_options_radio_selectors input[type="radio"] {
  position: absolute;
  opacity: 0;
  -moz-opacity: 0;
  -webkit-opacity: 0;
  -o-opacity: 0;
}

#send_options_radio_selectors input[type="radio"] + label {
  position: relative;
  margin-right: 12%;
  font-size: 1.5rem;
  line-height: 23px;
  text-indent: 25px;
  color: #505e6d;
  cursor: pointer;
}

#send_options_radio_selectors input[type="radio"] + label:before {
  content: "";
  display: block;
  position: absolute;
  top: 2px;
  height: 18px;
  width: 18px;
  background: white;
  border: 1px solid gray;
  box-shadow: inset 0 0 0 2px white;
  -webkit-box-shadow: inset 0 0 0 2px white;
  -moz-box-shadow: inset 0 0 0 2px white;
  -o-box-shadow: inset 0 0 0 2px white;
  -webkit-border-radius: 50px;
  -moz-border-radius: 50px;
  -o-border-radius: 50px;
}

#send_options_radio_selectors input[type="radio"]:checked + label:before {
  background: #e16846;
}

最佳答案

您可以“收听” change事件。然后你可以使用 .prop('checked) 检查特定的 radio 是否是 :checked (有很多方法可以检查这个)

$('[name="radio-group"]').on('change', function() {
  alert($('#sendlater').prop('checked'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" name="radio-group" id="sendnow" value="Send Now" checked>
<label for="sendnow" class="camp_lbl">Send Now</label>
<input type="radio" name="radio-group" id="sendlater" value="Schedule Send">
<label id="zzz" for="sendlater" class="camp_lbl">Schedule Send</label>

关于javascript - 选中单选按钮(输入)时如何触发事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36113272/

相关文章:

javascript - Angular 2 : How to implement hover contextual menu functionality

javascript - 在元素高度旁边滚动图像

html - 如何在 bootstrap 3.1 中将侧边栏固定到左侧

javascript - 我在登录表单中不断收到错误的用户名和密码

Javascript UWA 按钮样式

javascript - 新窗口中的 Highcharts

javascript - 查找所有英制单位并替换为相应的公制单位

jquery - 数据表ajax重新加载不更新传递的参数

javascript - 从 jQuery 到 vanilla JS

CSS 位置 div 在背景图像上的响应