Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 721
Hassaan Avatar asked Nov 28 '22 06:11

Hassaan


2 Answers

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);
});
like image 24
Hassaan Avatar answered Dec 10 '22 22:12

Hassaan


Just read the checked status in this way:

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

Fiddle Example

like image 61
maxdelia Avatar answered Dec 10 '22 21:12

maxdelia