Hassaan
Hassaan

Reputation: 7672

bootstrap-switch check box value always "on"

I am developing CMS and on Add Form I'm using Bootstrap-switch in my form. But It always return checked or on value.

JSFiddle

CODE

<html>
<head>
<link rel="stylesheet" type="text/css" href="http://www.bootstrap-switch.org/docs/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="http://www.bootstrap-switch.org/dist/css/bootstrap3/bootstrap-switch.css">
<script type='text/javascript' src="http://www.bootstrap-switch.org/docs/js/bootstrap.min.js"></script>
<script type='text/javascript' src="http://www.bootstrap-switch.org/dist/js/bootstrap-switch.js"></script>
<script type='text/javascript'>

$(window).load(function(){

    var s = $('#status').val();
    $("#status").change(function(){
        alert(s);
    });

});
</script>

</head>
<body>

    <input type="checkbox" id="status" name="status" checked>

</body>
</html>

Upvotes: 5

Views: 9835

Answers (6)

Ben
Ben

Reputation: 2033

You can use :checked in jquery

$('#switch').is(':checked')

Upvotes: 4

correct answer is

$("input[name='courseFreePaid']").bootstrapSwitch('state');

This will return true or false

Upvotes: 0

maxdelia
maxdelia

Reputation: 868

Just read the checked status in this way:

$("#status").change(function(){
    alert(this.checked);
});

Fiddle Example

Upvotes: 11

Happy Coding
Happy Coding

Reputation: 2525

Can go for this:

$(function(){
            $("#status").change(function(){
                if($(this).is(':checked')) { alert('on');}
                else alert('off');
            });
    });

Upvotes: 3

Mayank
Mayank

Reputation: 1392

Try The FIDDLE

You can use the following event to get the current value.

$('#status').bootstrapSwitch('state', true);


$('#status').on('switchChange.bootstrapSwitch', function (event, state) {

    alert(this);
    alert(event);
    alert(state);
    event.preventDefault();
});

Hope it helps

Upvotes: 3

Hassaan
Hassaan

Reputation: 7672

Change from

var s = $('#status').val();
$("#status").change(function(){
    alert(s);
});

Into

$("#status").change(function(){
    if(this.checked == true)
    {
        s = 'on';
    }
    else
    {
        s = 'off';        
    }
    alert(s);
});

Upvotes: 8

Related Questions