简体   繁体   English

如何在隐藏的输入中获取引导开关值

[英]How to get a bootstrap switch value in hidden input

I am using a bootstrap switch which looks like toggle box which toggle the two values in a click. 我正在使用引导开关,它看起来像切换框,可在单击时切换两个值。 Here is the switch code. 这是开关代码。

<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">

Whenever anyone clicks on this switch the switch toggle to left-right or right-left with the on off text. 每当有人单击该开关时,开关将切换为左右或左右,并带有关闭文本。

I want that "data-on-text" & "data-off-text" in the hidden box or at least the true false states. 我想要隐藏框中的“文本数据”和“文本数据”,或者至少是真正的错误状态。

This is the hidden input in which i want the value. 这是我想要值的隐藏输入。

<input class="prop_state" type="hidden" value="" id="prop_state">

I have tried some of this code to get the desired result. 我已经尝试了一些此代码以获得所需的结果。

    $(document).on('click', '#transaction_type_filter', function () {
    alert($('#transaction_type_filter').bootstrapSwitch('state'));
    alert($('#transaction_type_filter').bootstrapSwitch('toggleState'));
    console.log($('#transaction_type_filter').bootstrapSwitch('toggleState'));
    console.log($('#transaction_type_filter').bootstrapSwitch('state'));
});

and also tried some other scripts also. 并且还尝试了其他一些脚本。

 $(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');
    }
});
 });

I have not much experience of working in jquery or in js. 我没有在jquery或js中工作的经验。 I am just trying it. 我只是尝试。 Took me as a beginner. 以我为初学者。 Thanks for any suggestion in advance. 感谢您提前提出任何建议。

This is the jsfiddle 这是jsfiddle

Thanks for the help friends. 感谢您的帮助。 I got the correct way of getting the bootstrap switch state.Here is the code. 我获得了引导程序启动状态的正确方法,这是代码。

 $('#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);
    }
});

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

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