我有两个 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/