简体   繁体   English

当用户在打开和关闭之间切换时,如何获取Bootstrap Switch值?

[英]How to retrieve Bootstrap Switch value when user toggle between on and off?

I currently have a bootstrap switch that allow users to toggle between "yes" or "no". 我目前有一个引导开关,允许用户在“是”或“否”之间切换。 I also have a "send" button. 我也有一个“发送”按钮。 If the user toggle to the "yes" option, I want to collect a "true" value. 如果用户切换到“是”选项,我想收集一个“真”值。 If user toggle to the "no" option, I want to collect a "false" value and then send it to my Post web api method. 如果用户切换到“否”选项,我想收集一个“假”值,然后将其发送到我的Post Web api方法。 在此处输入图片说明 However, I was only able to receive the "true" value even when the user toggle to "no" 但是,即使用户切换为“否”,我也只能接收“ true”值

 $("#trueInput").bootstrapSwitch(); $('#saveButton').on('click', function () { var collectedInput = $("#trueInput").val(); }); 
 <input type="checkbox" id="trueInput" name="trueInput value="true" checked/> 

在此处输入图片说明

This will solve your problem. 这样可以解决您的问题。

  $('[type="radio"]').click(function () { var isChecked = $(this).val(); alert(isChecked); }); 
  .btn-default.btn-on.active { background-color: #5BB75B; color: white; } .btn-default.btn-off.active { background-color: #DA4F49; color: white; } 
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <div class="container" style="margin-top:20px;"> <div class="row"> <div class="btn-group" id="status" data-toggle="buttons"> <label class="btn btn-default btn-on btn-xs active"> <input type="radio" value="true" name="on" checked="checked">ON</label> <label class="btn btn-default btn-off btn-xs "> <input type="radio" value="false" name="off">OFF</label> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM