Javascript 如何在隐藏输入中获取引导开关值
我使用的是一个引导开关,它看起来像一个切换框,可以在一次点击中切换两个值。这是开关代码Javascript 如何在隐藏输入中获取引导开关值,javascript,jquery,bootstrap-switch,Javascript,Jquery,Bootstrap Switch,我使用的是一个引导开关,它看起来像一个切换框,可以在一次点击中切换两个值。这是开关代码 <input class="bt-switch transaction_type_filter" type="checkbox" checked data-on-text="Buy" name="transaction_type_filter" id="transaction_type_filter" data-off-text="Rent" data-on-color="primary" data-o
<input class="bt-switch transaction_type_filter" type="checkbox" checked data-on-text="Buy" name="transaction_type_filter" id="transaction_type_filter" data-off-text="Rent" data-on-color="primary" data-off-color="warning">
还尝试了其他一些脚本
$(document).ready(function() {
$('.transaction_type_filter').on('switch-change', function (e, data) {
var $el = $(data.el)
, value = data.value;
if($('#transaction_type_filter').bootstrapSwitch('state') === true){//this is true if the switch is on
alert('true');
}else{
alert('false');
}
});
});
我没有太多使用jquery或js的经验。我只是想试试。把我当作初学者。提前谢谢你的建议
这是感谢朋友们的帮助。我得到了获取引导开关状态的正确方法。下面是代码
$('#transaction_type_filter').on('change.bootstrapSwitch', function (e, state) {
console.log(e.target.checked);
alert(e.target.checked);
if (e.target.checked == true) {
$value = 'buy';
$('input.prop_state').val($value);
} else {
$value = 'rent';
$('input.prop_state').val($value);
}
});
您只需要$(“#prop_state”).val($(“#transaction_type_filter”).bootstrapSwitch('state');我尝试了该选项,但如果用户单击开关两次,则无法获得值。我尝试了此代码,但未获得更改的值,仅获得一个值。请将代码放入JSFIDLE并在此处共享链接,使我们能够更轻松地立即检查和调试代码。我添加了一个JSFIDLE。在这个脚本中,如果我必须更改切换颜色怎么办??
$(document).ready(function() {
$('.transaction_type_filter').on('switch-change', function (e, data) {
var $el = $(data.el)
, value = data.value;
if($('#transaction_type_filter').bootstrapSwitch('state') === true){//this is true if the switch is on
alert('true');
}else{
alert('false');
}
});
});
$('#transaction_type_filter').on('change.bootstrapSwitch', function (e, state) {
console.log(e.target.checked);
alert(e.target.checked);
if (e.target.checked == true) {
$value = 'buy';
$('input.prop_state').val($value);
} else {
$value = 'rent';
$('input.prop_state').val($value);
}
});