2010-08-15 142 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