获取选中的引导程序复选框的值

Bra*_*ype 5 html javascript jquery

我有这个HTML表单:

<div class="animated-switch">
  <input id="switch-success" name="switch-success" checked="" type="checkbox">
  <label for="switch-success" class="label-success"></label>
</div>  
Run Code Online (Sandbox Code Playgroud)

我怎么知道输入是否被检查?

我尝试过这样,但没有任何提示:

$(document).on('click','.animated-switch',function(e){
    alert($('#switch-success:checked').val() );
});
Run Code Online (Sandbox Code Playgroud)

这是一个JSFIDDLE文件,以查看我的清晰示例:https ://jsfiddle.net/s2f9q3fv/

Nee*_*mar 9

如果选中此复选框,则可以得到。

       $('#' + id).is(":checked")
Run Code Online (Sandbox Code Playgroud)


Kri*_*iev 4

您的代码在代码片段中运行良好。您忘记向复选框添加值,但即使没有它,您也应该能够看到具有未定义值的警报消息(请参阅下面的代码片段中添加的值“test”)。

$(document).on('click','#switch-success:checked',function(e){
    alert($('#switch-success:checked').val() );
});
Run Code Online (Sandbox Code Playgroud)
.animated-switch > input[type="checkbox"] {
  display: none; }

.animated-switch > label {
  cursor: pointer;
  height: 0px;
  position: relative;
  width: 40px; }

.animated-switch > label::before {
  background: black;
  box-shadow: inset 0px 0px 10px rgba(0, 0, 0, 0.5);
  border-radius: 8px;
  content: '';
  height: 16px;
  margin-top: -8px;
  position: absolute;
  opacity: 0.3;
  transition: all 0.4s ease-in-out;
  width: 40px; }

.animated-switch > label::after {
  background: white;
  border-radius: 16px;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
  content: '';
  height: 24px;
  left: -4px;
  margin-top: -8px;
  position: absolute;
  top: -4px;
  transition: all 0.3s ease-in-out;
  width: 24px; }

.animated-switch > input[type="checkbox"]:checked + label::before {
  background: inherit;
  opacity: 0.5; }

.animated-switch > input[type="checkbox"]:checked + label::after {
  background: inherit;
  left: 20px; }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<div class="animated-switch">
  <input id="switch-success" name="switch-success" checked="" value="test" type="checkbox">
  <label for="switch-success" class="label-success"></label>
</div>
Run Code Online (Sandbox Code Playgroud)

更新:您应该将单击事件绑定到复选框本身,而不是父级。再次尝试使用精美的动画进行剪辑。请注意,如果您希望始终执行警报,则必须删除:checked选择器中的属性。