i have 2 txt fields named stuno and stuname . when i enter student no in the txtfield1 it pulls and shows the student name in the txtfield2. so here is what iam doing onload of the document iam putting the focus on txtfield1(stuno).If i dont put the focus it is pulling the Name very fine . IF i have the focus it is not pulling.Onload of the form
i need to have focus on the txtfield1(stuno).Here is the code which i have please correct me if iam doing something wrong.
$(document).ready(function(){
$("#stuno").focus();
$("#stuno").change(function(){
if ( jQuery.trim( $( '#stuno' ).val() ) == '' ) {
$('#stuname').val( '' );
$( '#stuerr' ).text( 'Student Account Not Valid!' ) ;
}
else{
$.ajaxSetup ({cache: false});
$.getJSON("student.php",{'stuno' : $("#stuno").attr('value')},
function(data){
if(data[0].status){
var labelvalue = data[0].label.split('-');
$("#stuname").attr("value",labelvalue[1]);
}else{
$('#stuname').val( '' );
$( '#stuerr' ).text(data[0].label);
}
if( $("#stuname").attr("value") ){
$( '#stuerr' ).text( '' ) ;
}
});
}
});
});
}
Try using
keypressinstead ofchange. Change is fired when you leave focus of the input or when a select box is changed. Keypress is fired whenever a key is pressed or released.So instead of:
use