1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| window.onload=init;
function init()
{
$('cp').onkeyup=citySeek;
}
function citySeek(evt)
{
if(this.value.length==5)
{
new Ajax.Request('city-chooser.php',
{
method: 'post',
contentType: 'application/x-www-form-urlencoded',
charset: 'UTF-8',
parameters: 'zip='+this.value,
onSuccess: function(transport)
{
var root = transport.responseXML.firstChild;
if(root.childNodes.length > 2)
{
var input = document.createElement('select');
input.className='inputBox';
input.id='ville';
for(var i = 0 ; i <= root.childNodes.length - 1 ; i++)
{
var city = root.childNodes[i].firstChild.nodeValue;
var elt = document.createElement('option');
var label = document.createTextNode(city);
elt.value=city;
elt.appendChild(label);
var att= document.createAttribute('style');
att.value='width: 126px';
input.setAttributeNode(att);
input.appendChild(elt);
}
}
else if(root.childNodes.length == 1)
{
var input = document.createElement('input');
input.id='ville';
input.className='inputBox';
input.type='text';
input.value=root.childNodes[0].firstChild.nodeValue
var att= document.createAttribute('readonly');
att.value='readonly';
input.setAttributeNode(att);
var att= document.createAttribute('style');
att.value='width: 120px';
input.setAttributeNode(att);
}
else
{
alert('Veuillez saisir un code postal valide');
var input = document.createElement('input');
input.id='ville';
input.className='inputBox';
input.type='text';
input.value='Aucune ville correspondante';
var att= document.createAttribute('readonly');
att.value='readonly';
input.setAttributeNode(att);
var att= document.createAttribute('style');
att.value='width: 120px';
input.setAttributeNode(att);
}
$('puter').replaceChild(input,$('ville'));
}
});
}
} |
Partager