[英]How to get value of bootstrap switch
I'm using bootstrap switch & i want to get checkbox value on click on switch.我正在使用引导程序开关,我想在单击开关时获取复选框值。 I've tried this but i din't get the value.我试过这个但我没有得到价值。 Could you check my code bellow & let me know where i did wrong你能检查一下我的代码并让我知道我哪里做错了吗
$('.make-switch').bootstrapSwitch('state'); $('#price_check').click(function () { //alert('Test'); var check = $(this).val(); console.log(check) });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script> <div class="margin-bottom-10"> <input type="checkbox" class="make-switch" id="price_check" name="pricing" data-on-color="primary" data-off-color="info" value="true"> </div>
Click events don't get triggered with switches.单击事件不会被开关触发。 Try to use the event switchChange.bootstrapSwitch .尝试使用事件switchChange.bootstrapSwitch 。 This one gets triggered when changing a switch.更改开关时会触发此操作。
Also to check if it's on, I check if the class bootstrap-switch-on is applied.还要检查它是否打开,我检查是否应用了类 bootstrap-switch-on。 If not than the switch if off.如果不是比开关如果关闭。
$('.make-switch').bootstrapSwitch('state'); $('.make-switch').on('switchChange.bootstrapSwitch',function () { var check = $('.bootstrap-switch-on'); if (check.length > 0) { console.log('ON') } else { console.log('OFF') } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script> <div class="margin-bottom-10"> <input type="checkbox" class="make-switch" id="price_check" name="pricing" data-on-color="primary" data-off-color="info" value="true"> </div>
Tamara,塔玛拉,
Bootstrap Switch has an onSwitchChange
property that will be able to give you the state of the toggle. Bootstrap Switch 有一个onSwitchChange
属性,可以为您提供切换状态。 Please see this fiddle: https://jsfiddle.net/learnwithclyde/to2hng3f/请看这个小提琴: https : //jsfiddle.net/learnwithclyde/to2hng3f/
$("#price_check").bootstrapSwitch({
onSwitchChange: function(e, state) {
alert(state);
}
});
The above code snippet is how you would implement onSwitchChange
property and get the state
of the toggle control.上面的代码片段是您将如何实现onSwitchChange
属性并获取切换控件的state
。
this code worked for me这段代码对我有用
$('#price_check').on('switchChange.bootstrapSwitch', function (e, data) {
alert($('#price_check').bootstrapSwitch('state'));
});
Try this, you will get the value of checked element..试试这个,你会得到检查元素的值..
$("#boot_switch").bootstrapSwitch({ onSwitchChange: function(e) { alert(e.target.value); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script> <div class="margin-bottom-10"> <input type="checkbox" class="make-switch" id="boot_switch" name="pricing" data-on-color="success" data-off-color="info" value="value1"> </div>
I guess all these solutions are old news as of v5?我猜所有这些解决方案都是 v5 的旧新闻?
in any case none of these work for me.无论如何,这些都不适合我。 So I used this solution所以我使用了这个解决方案
$('#orderSwitch').on('change', function() {
console.log($(this).is(':checked'));
});
$("#price_check").change(function () {
var price = false;
if ($(this).prop('checked') == true) {
price = true;
} else {
price = false;
}
}
try this one to get value (true or false)试试这个来获得价值(真或假)
$('#boot_switch').on('switchChange.bootstrapSwitch', function (event, status) {
console.log(status);
});
$("#price-check").prop("checked") // true / false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.