2013-05-18 37 views
-3

我試圖創造一個參數q將被動態定義一個Ajax函數調用,而$.get函數將返回取決於由阿賈克斯返回的數據truefalse呼叫。

<a href="any.php" class="idd" id="1">add score</a> 
<a href="any.php" class="idd" id="2">add score</a> 
<a href="any.php" class="idd" id="3">add score</a> 
$(".idd").click(function(event) { 
window.Eid=event.target.id; 
//alert(window.Eid); 
}); 

    $.get("getDetails.php", 
     { q: window.Eid }, // I want this variable dynamic 
     function(m) { 
      $(".idd").click(function() { 
      //var Eid=event.target.id; 
      alert(m); 
      if(m>0) { 
       return false; 
      } else { 
       return true; 
       } 
    });  
    }); 

回答

1
$(function(){ 
    $(".idd").click(function(event) { 
     var id = this.id; 
     var ajaxRequest = $.get("getDetails.php", {q: id}); 
     ajaxRequest.done(function(m){ 
      alert(m); 
      if (m > 0) { 
       // do sth here 
      } else { 
       //do another things here 
      } 
     });  
     ajaxRequest.fail(function(){ 
      alert('failed'); 
     }); 
     return false; 
    }); 
}); 
window.onerror = function(errorMessage, url, line) { 
    var errorText = 'message: ' + errorMessage + '\nurl: ' + url + '\nline: ' + line; 
    alert(errorText); 
} 
+0

謝謝鮑勃你是最好的編碼器。 –