twitter-bootstrap Bootstrap 开关按钮事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38043118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap switch button event
提问by Mike Henley
I'm using bootstrap switch button. I want to change input value when on text or off text is changed. How can I check if on-text="Android" I want to print android to inp1 text or if off-text="IOS" I want to print ios to inp1 text. ?s there another way to check text? You can check online demo http://jsfiddle.net/PNU45/156/
我正在使用引导程序开关按钮。我想在更改打开文本或关闭文本时更改输入值。如何检查 on-text="Android" 我想将 android 打印到 inp1 文本还是 off-text="IOS" 我想将 ios 打印到 inp1 文本。?还有另一种检查文本的方法吗?您可以查看在线演示http://jsfiddle.net/PNU45/156/
HTML
HTML
<input type="input" id="inp1" value="">
<input type="input" id="inp2" value="">
<input type="checkbox" checked="true" class="alert-status" id="btn1" data-size="normal" name="my-checkbox" data-on-text="Android" data-off-text="IOS">
<input type="checkbox" checked="true" class="alert-status" id="btn2" data-size="normal" name="my-checkbox" data-on-text="Java" data-off-text="C#">
JavaScript
JavaScript
$('.alert-status').bootstrapSwitch('state', true);
$('#btn1').on('switchChange.bootstrapSwitch', function (event, state) {
var x=$(this).data('on-text');
var y=$(this).data('off-text');
if(x=="Android"){
$("#inp1").val("Android");
}
if(y=="IOS"){
$("#inp1").val("IOS");
}
});
$('#btn2').on('switchChange.bootstrapSwitch', function (event, state) {
var x=$(this).data('on-text');
var y=$(this).data('off-text');
if(x=="Java"){
$("#inp2").val("Java");
}
if(y=="IOS"){
$("#inp2").val("IOS");
}
});
回答by max
You can check if input is checked:
您可以检查输入是否被选中:
$('#switch').on('switchChange.bootstrapSwitch', function (event, state) {
var x = $(this).data('on-text');
var y = $(this).data('off-text');
if($("#switch").is(':checked')) {
$("#in0p1").val(x);
} else {
$("#inp1").val(y);
}
});
$('#switch2').on('switchChange.bootstrapSwitch', function (event, state) {
var x = $(this).data('on-text');
var y = $(this).data('off-text');
if($("#switch2").is(':checked')) {
$("#inp2").val(x);
} else {
$("#inp2").val(y);
}
});
JSFIDDLE
JSFIDDLE
回答by Kld
You can use $(selector).bootstrapSwitch('state');to get the status of the switch
您可以使用$(selector).bootstrapSwitch('state');来获取开关的状态
$('.alert-status').bootstrapSwitch('state', true);
$(document).on('switchChange.bootstrapSwitch', '.alert-status', function (event, state) {
if ($(this).bootstrapSwitch('state')) {
console.log($(this).data('on-text'));
}else {
console.log($(this).data('off-text'));
}
});

