var titles = ['Mr', 'Mrs', 'Dr', 'Master'];
var countries = ['Usa', 'Russia', 'England', 'India', 'Australia'];
var cardTypes = ['Master', 'Visa', 'Diner\'s Club', 'American Express'];
function post(data) {
var dt = {
json: JSON.stringify(data),
delay: 6
};
return $.post('/echo/json/', dt);
}
$(function() {
$('button').click(function() {
var txt = $(this).text();
if (txt == 'Reload Page') {
location.reload();
}
else {
loadForm();
$(this).text('Reload Page');
}
});
});
/*
*Doing all the loading here
*/
function loadForm() {
var counter = 0;
log('loadForm executed');
function update() {
log('update() called');
if (++counter == 3) {
$('div.modal').dialog('destroy');
}
}
var modal = $('<div/>').addClass('modal').html('Loading drop down items...').attr('title', 'Loading');
$(document.body).append(modal);
$('div.modal').dialog({
open: function(event, ui) {
log('dialog open');
$(".ui-dialog-titlebar-close").hide();
},
modal: true
});
post(titles).success(function(dt) {
loadTitles(dt);
update();
});
post(countries).success(function(dt) {
loadCountries(dt);
update();
});
post(cardTypes).success(function(dt) {
loadCardTypes(dt);
update();
});
}
function loadTitles(data) {
$.each(data, function(i, title) {
var op = $('<option/>').html(title);
$('#ctitle').append(op);
});
}
function loadCountries(data) {
$.each(data, function(i, title) {
var op = $('<option/>').html(title);
$('#ccntry').append(op);
});
}
function loadCardTypes(data) {
$.each(data, function(i, title) {
var op = $('<option/>').html(title);
$('#cctype').append(op);
});
}
function log(msg) {
if (typeof console != 'undefined') console.log(msg);
}
<h1>Dummy Customer Entry Form</h1>
<button>Load Form</button>
<table>
<tr>
<td>
Title:
<p>
<select id="ctitle">
</select>
</p>
</td>
<td>
Last Name:
<p><input type="text" id="clname"/></p>
</td>
<td>
First Name:
<p><input type="text" id="cfname"/></p>
</td>
</tr>
<tr>
<td>
Street 1:
<p><input type="text" id="castr1"/></p>
</td>
<td>
Street 2:
<p><input type="text" id="castr2"/></p>
</td>
<td>
City:
<p><input type="text" id="ccity"/></p>
</td>
</tr>
<tr>
<td>
State:
<p><input type="text" id="cstate"/></p>
</td>
<td>
Country:
<p>
<select id="ccntry">
</select>
</p>
</td>
</tr>
<tr>
<td>
Card Type:
<p><select id="cctype"></select></p>
</td>
<td>
Card No:
<p><input type="text" id="ccardno"/></p>
</td>
<td>
Expiry (MM/yyyy):
<p><input type="text" id="ccexpiry"/></p>
</td>
</tr>
</table>
<input type="button" value="submit"/>
body{
font-size:10pt;
background-color:#FFCCFF;
font-family:Arial;
}
h1{
font-size:16pt;
font-weight:bold;
}
External resources loaded into this fiddle: