Bootstrap switch checked event?

前端 未结 9 1539
感情败类
感情败类 2021-01-31 14:48

I use this codes for checkbox checked event but it does not work.

css



        
相关标签:
9条回答
  • 2021-01-31 15:20

    try this

    <input type="checkbox" name="my-checkbox" checked>
    
        $('input[name="my-checkbox"]').on('switchChange.bootstrapSwitch', function (event, state) 
            {
                  if (state == true) 
                   {
                     //your code
                   }
                   else
                   {
                      //your code
                   }
            });
    
    0 讨论(0)
  • 2021-01-31 15:25

    This is the way that I use it:

    HTMl:

    <input name="field_name" class="switcher" type="checkbox" data-size="small" value="1">
    

    JQuery:

    $('.switcher').on('switchChange.bootstrapSwitch', function(event, state) {
        if (state)
        {
            // Checked
        }else
        {
            // Is not checked
        }
    });
    

    I hope it is helpful!!

    0 讨论(0)
  • 2021-01-31 15:26

    The documentation provides the events for the bootstrap-switch. But here is an example that uses a single checkbox as well as a radio group just for completeness. I have also thrown in the Disable method as well.

    $('#TheCheckBox').on('switchChange.bootstrapSwitch', function () {
       $("#CheckBoxValue").text($('#TheCheckBox').bootstrapSwitch('state'));
    });
    
    0 讨论(0)
  • 2021-01-31 15:31

    Try this worked for me

     $('#check').change(function (event) {
        var state = $(this).bootstrapSwitch('state');
        if (state) {
           // true
        } else {
           // false
        }
        event.preventDefault();
    });
    
    0 讨论(0)
  • 2021-01-31 15:33

    For me this was the only working code (Bootstrap 3.0) :

    $('#my_checkbox').on('change.bootstrapSwitch', function(e) {
        console.log(e.target.checked);
    });
    

    It returns me true or false

    Example with Ajax submission on bootstrap switch change :

    $('#my_checkbox').on('change.bootstrapSwitch', function(e) {
        $.ajax({
            method: 'POST',
            url: '/uri/of/your/page',
            data: {
                'my_checkbox_value': e.target.checked
            },
            dataType: 'json',
            success: function(data){
                console.log(data);
            }
        });
    });
    
    0 讨论(0)
  • 2021-01-31 15:33

    You should call the switch-change function like below

    $('.switch').on('switch-change', function () {
        console.log("inside switchchange");
        toggleWeather();
    });
    

    If you are seeing to create it dynamically, follow the steps given in the link which was previously posted by me. [ create a radio bootstrap-switch dynamically ]

    This is for radio type. For checkbox type you can change the type='checkbox'.

    You can refer to the below link for more examples - http://www.bootstrap-switch.org/

    0 讨论(0)
提交回复
热议问题