Reputation: 7672
I am developing CMS and on Add Form I'm using Bootstrap-switch in my form. But It always return checked
or on
value.
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
Reputation: 51
correct answer is
$("input[name='courseFreePaid']").bootstrapSwitch('state');
This will return true or false
Upvotes: 0
Reputation: 868
Just read the checked status in this way:
$("#status").change(function(){
alert(this.checked);
});
Upvotes: 11
Reputation: 2525
Can go for this:
$(function(){
$("#status").change(function(){
if($(this).is(':checked')) { alert('on');}
else alert('off');
});
});
Upvotes: 3
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
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