2015-10-16 2 views
0

я написал функцию для проверки имени пользователя на моем сайтеAjax запрос обратного вызова вызывает другую функцию

//Check username 
//Check if the length is greater than 3 and no username already exists. Several other checks included 

$('#username').blur(function() 
{ 

     var v=this.value; 
     if(v == "") 
     { 
      addPopover(this,'This field is required!'); 
      $('#username_group').removeClass("has-success").addClass("has-error"); 
      $('#username_feedback').removeClass("glyphicon-ok").addClass("glyphicon-remove"); 
     } 
     else if(username.length < 4) 
     { 
      addPopover(this,'The username must be atleast 4 characters long'); 
      $('#username_group').removeClass("has-success").addClass("has-error"); 
      $('#username_feedback').removeClass("glyphicon-ok").addClass("glyphicon-remove"); 
     } 
     else 
     { 
      $.post('php/check_username_avail.php',{ username : v },function(data){ 

        if(data == "false") 
        { 
         alert('false occurred'); 
         addPopover(this,'Sorry! this username is already taken.'); 
         $('#username_group').removeClass("has-success").addClass("has-error"); 
         $('#username_feedback').removeClass("glyphicon-ok").addClass("glyphicon-remove"); 
         alert('false ended'); 
        } 
        else if(data == "true") 
        { 
         alert('true occurred'); 
         removePopover(this); 
         $('#username_group').addClass("has-success").removeClass("has-error"); 
         $('#username_feedback').addClass("glyphicon-ok").removeClass("glyphicon-remove"); 
         alert('true ended'); 
        } 
        else 
        { 
         alert('else occurred!'); 
         addPopover(this,data); 
         $('#username_group').removeClass("has-success").addClass("has-error"); 
         $('#username_feedback').removeClass("glyphicon-ok").addClass("glyphicon-remove"); 
         alert('else ended'); 
        } 
       }).error(function(){ 
        alert("An error occurred. Unable to validate username"); 
       }); 
     } 
}); 


//function to add popover 
function addPopover(id,message) 
{ 
    alert("in add popover"); 
    $(id).attr("data-toggle","popover"); 
    $(id).attr("data-trigger","focus"); 
    $(id).attr("data-placement","left"); 
    $(id).attr("data-content",message); 
    $(id).popover(); 
    alert('add popover ended'); 
} 

//function to remove popover 
function removePopover(id) 
{ 
    alert("in remove popover"); 
    $(id).removeAttr("data-toggle"); 
    $(id).removeAttr("data-trigger"); 
    $(id).removeAttr("data-placement"); 
    $(id).removeAttr("data-content"); 
    alert("remove popover ended"); 
} 

Когда данные ложно, я вижу 4 предупреждения, то есть «ложь произошло», «в надстройке пирог»,» add popover закончился "и" false закончился "в том же порядке.

но все же атрибуты, которые я добавил в функции addPopover, не добавляются. Почему это так?

+0

Можете ли вы создать скрипку, чтобы мы могли легче протестировать? – polydor

+0

@Mestrum Я не знаю, как создать скрипку для запросов ajax. – Raman

ответ

1

«эта» функция обратного вызова внутри почты не является тем, что вы ожидаете.

$('#username').blur(function() { 
    var that = this, 
     v = this.value; 
    //... 
    $.post('php/check_username_avail.php', { username : v }, function(data) { 
     //... 
     addPopover(that,'Sorry! this username is already taken.'); 
     //... 
    } 
    //... 
} 
+0

Спасибо. Это было слишком глупо. – Raman

Смежные вопросы