2010-08-15 160 views
0
$('.updateDescriptionProduct').click(function() { 
    if (updateProductDescription(productID, description) == 'true') { 
    alert('success!'); 
    } else { 
    alert('did not update');   
    } 
}); 

function updateProductDescription(productID, description) { 
    $.ajax({ 
    url: '/index.php/updateProductDescription', 
    global: false, 
    type: 'POST', 
    data: ({productID: productID, description: description}), 
    dataType: 'html', 
    async:false, 
    success: function(msg){ 
     alert(msg); 

     return msg; 
    } 
    }); 
} 

函數本身說的是對的,但是我的click事件返回爲undefinedjQuery Ajax返回undefined

+0

你究竟在做什麼? – 2010-08-15 01:05:26

回答

10

return適用於回調。嘗試在初始函數中設置一個變量並在回調中設置該值,然後將其返回?

function updateProductDescription(productID, description) { 
    var ret = false; 
    $.ajax({ 
      url: '/index.php/updateProductDescription', 
      global: false, 
      type: 'POST', 
      data: ({productID: productID, description: description}), 
      dataType: 'html', 
      async:false, 
      success: function(msg){ 
      ret = msg; 
      } 
    }); 
    return ret; 
} 

我還沒有做過async調用,但它似乎應該工作。讓我知道如果它不。

+1

但他用'async:false'禁用了它。否則,他不得不重構它,因爲他在異步做它。 – 2010-08-15 01:06:08

+0

是的,抱歉沒有注意到。 +1 – 2010-08-15 01:07:12

+0

就是這樣!感謝您及時的回覆。 – Chris 2010-08-15 01:10:32