用户在打开和关闭之间切换时如何检索Bootstrap Switch值?

时间:2018-06-07 08:44:35

标签: javascript jquery asp.net-core

我目前有一个自举开关,允许用户在"是"之间切换。或"不"。我也有一个"发送"按钮。如果用户切换到"是"选项,我想收集一个" true"值。如果用户切换到" no"选项,我想收集一个" false"值,然后将其发送到我的Post web api方法。enter image description here但是,我只能收到" true"即使用户切换到" no"



$("#trueInput").bootstrapSwitch();
$('#saveButton').on('click', function () {
var collectedInput = $("#trueInput").val();
});

<input type="checkbox" id="trueInput" name="trueInput value="true" checked/>
&#13;
&#13;
&#13;

enter image description here

1 个答案:

答案 0 :(得分:0)

这将解决您的问题。

&#13;
&#13;
            $('[type="radio"]').click(function () {
                var isChecked = $(this).val();
                alert(isChecked);
            });
 
&#13;
        .btn-default.btn-on.active {
            background-color: #5BB75B;
            color: white;
        }

        .btn-default.btn-off.active {
            background-color: #DA4F49;
            color: white;
        }
&#13;
    <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>
&#13;
&#13;
&#13;