Bootstrap 3 Template
This is a simple Twitter Bootstrap 3 template. You can fork it to get a ready to use Bootstrap 3 fiddle.
by Jeff Beagley
HTML
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.0-alpha/css/bootstrap-select.min.css" />
<div class="form-group">
<label class="control-label col-sm-1 no-right-padding no-left-padding" for="tstate">State:</label>
<div class="col-sm-5 col-md-5 col-lg-5 no-right-padding">
<select class="selectpicker state" id="state">
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 no-right-padding no-left-padding" for="tcountry">Country:</label>
<div class="col-sm-6 col-md-6 col-lg-6 no-right-padding">
<select class="selectpicker country" id="country" dropupAuto=false>
<option value=1>US</option>
<option value=2>Canada</option>
</select>
</div>
</div>
<button type="button" class="btn btn-primary" id="load">Primary</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.0-alpha/js/bootstrap-select.min.js"></script>
CSS
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
JavaScript
/* Latest compiled and minified JavaScript included as External Resource */
$(document).ready(function() {
$( "#country" ).on( 'changed.bs.select', function(e) {
change_country($( this ).val());
});
$( "#load" ).click(function() {
select_data();
});
});
function change_country(country_id) {
$.ajax({
type: 'GET',
cache: false,
url: '/echo/jsonp/',
success: function(response) {
$( "#state" ).empty();
if(country_id == 1) {
$( "#state" ).append( "<option value=1>Oklahoma</option" );
$( "#state" ).append( "<option value=2>Missouri</option" );
} else {
$( "#state" ).append( "<option value=1>Ontario</option" );
$( "#state" ).append( "<option value=2>Quebec</option" );
}
$( "#state" ).selectpicker( "refresh" );
}
});
}
function select_data() {
var country_id = 2
var state_id = 2
$( "#country" ).selectpicker('val', country_id);
$( "#state" ).selectpicker('val', state_id);
}