Jump to content

regarding ajax


funbinod

Recommended Posts

hi guys! greetings!

 

I've used ajax to get some data after user inputs some information on a <input> and hits enter.

function getAid(){var XMLHttpRequestObject = false;if (window.XMLHttpRequest) {	XMLHttpRequestObject = new XMLHttpRequest();} else if (window.ActiveXObject) {	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp");}XMLHttpRequestObject.onreadystatechange = function(){	if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {		document.getElementById('aid').value = XMLHttpRequestObject.responseText;	}}var name = encodeURIComponent(document.getElementById('name').value);XMLHttpRequestObject.open('POST', 'ajax/aid.php');XMLHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");XMLHttpRequestObject.send("name=" + name);}

this function is set onBlur() method on #name field. its working fine. but I wish something more. I wish that the cursor should move to another field only after the successful ajax callback, or say only after getting data on #aid field.

 

for example

 

<input id="name">

<input id="address">

<input id="aid">

 

when user inputs 'name' and hits 'enter', value is derived from ajax request to 'aid' field and after getting the value, the 'address' field with get the focus..

 

please guide through...

 

thanks in advance....

Link to comment
Share on other sites

ok

I've done this

	$('#name').keyup(function(e) {		getAid();        // this is the function for ajax request		$('#address').attr('disabled', 'disabled');		if (e.which === 13) {			if( ($('#aid').val() !== '')) {				e.preventDefault();				$('#address').removeAttr('disabled');				$('#address').focus();			} else {                                alert('Error!');				$('#name').select();			}		}        });

please guide me where I've done mistake. everything seems to be working fine but the focus goes to #address whether the ajax is successful or not...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...