2013-02-26 40 views
2

我有一些代碼,如下內嵌兩個JavaScript函數:退出的另一個功能

$(document).on('click', '.set_up_btn', function(){ 
var menu_name=$('.menu_name').val(); 
var menu_type=$('.menu_type_sel').val(); 
var rest_id=$('body').data('rest_id'); 
var error=false; 

if(menu_name=="") 
{ 
    $('.error_box13').eq(0).html("<p>Enter Identifier for this Menu</p>"); 
    $('.error_box13').eq(0).show(300); 
    error=true; 
} 
if(menu_type=="0") 
{ 
    $('.error_box13').eq(1).html("<p>Select Menu Type</p>"); 
    $('.error_box13').eq(1).show(300); 
    error=true; 
} 
if(error==true) 
{ 
    return; 
} 
//check menu name is unique 
var data="rest_id="+rest_id+"&name="+menu_name; 

$.ajax({ 
    type:"POST", 
    url:"includes/check_menu_name.php", 
    data:data, 
    success:function(html){ 
     if(html==1) 
     { 
      //menu name is duplicate 
      $('.error_box13').eq(0).html("<p>Menu Name already in use.</p>"); 
      $('.error_box13').eq(0).show(300); 
      error=true; 
     } 
     else if(html==2) 
     { 
      //menu name is ok 

     } 
    return error; 
    } 
});//end ajax 
if(error==true) 
{ 
    e.stopImmediatePropagation(); 
    return false; 
} 

var data2="rest_id="+rest_id+"&name="+menu_name+"&menu_type="+menu_type; 

$.ajax({ 
    type:"POST", 
    url:"includes/set_menu.php", 
    data:data2, 
    success:function(html2){ 
     if(html2==1) 
     { 
      var app=""; 
      if(menu_type==1) 
      { 
       menu_type="Bar Menu"; 
      } 
      if(menu_type==2) 
      { 
       menu_type="Food Menu"; 
      } 
      app+="<h2 class='menu_header'>"+menu_type+"</h2><hr />"; 
      app+="<h2 class='menu_title'>"+menu_name+"</h2>"; 
      $(app).appendTo($('.third').eq(2)); 
     } 
     else 
     { 
      alert("ERROR: Could not write to database. Please try again."); 
     } 
    } 
});//end ajax 

}); 

我需要做的是退出整個「點擊」功能,如果第一個Ajax請求返回「1」 - 菜單名稱已被該用戶使用。

目前,如果發生這種情況,javascript會繼續運行到第二個Ajax請求並將數據寫入數據庫。

+0

AJAX = ** **異步JavaScript和XML – undefined 2013-02-26 11:45:54

+1

你不能做到這一點。點擊事件處理程序不會等待AJAX​​請求被觸發或正在執行回調。那些是異步的。 – marekful 2013-02-26 11:46:01

回答

1

您遇到此問題是因爲成功函數在請求返回時在「稍後」調用。這不是即時的,所以錯誤變量還沒有被填充。處理成功函數中的錯誤如下:

$.ajax({ 
    type:"POST", 
    url:"includes/check_menu_name.php", 
    data:data, 
    success:function(html){ 
     if(html==1) 
     { 
      //menu name is duplicate 
      $('.error_box13').eq(0).html("<p>Menu Name already in use.</p>"); 
      $('.error_box13').eq(0).show(300); 
      error=true; 


      // we must handle the error here! 


      handleError(e, error); 
     } 
     else if(html==2) 
     { 
      //menu name is ok 



      // continue on as normal 
      continueNoError(); 
     } 
    return error; 
    } 
});//end ajax 

// Code run here runs BEFORE $.ajax returns 


function handleError(e, error) { 
    if(error==true) 
    { 
     e.stopImmediatePropagation(); 
     return false; 
    } 
} 

function continueNoError() { 
    // Put what to after the first ajax call here 
} 
0

目前第二個AJAX調用會運行任何情況。

將它包裝在您點擊的方法中,當滿足要求時您會調用它。

即:

if (menu_type === 2) { 
    menu_type="Food Menu"; 
    setMenu(); 
} 

則:

var setMenu = function() { 
    $.ajax({ 
     // do stuff 
    }); 
}