2014-03-19 98 views
-2

假设我有一个我可以调用AJAX使用元素ID,而不是在任何情况下

if(ond1satisfied){ 
<div id="checkA"> 
</div>} 



    if(cond2satisfied){ 
<div id="checkB"> 
</div>} 

我可以调用AJAX如

 $.ajax({ 
     url: 'example.php', 
     type: 'GET', 

    success: function(data) { 
//called when successful 
$('#ajaxphp-results').html(data); 
    }, 
     error: function(e) { 
//called when there is an error 
//console.log(e.message); 
    } 
     }); 

$("#checkA").ajax({ 
     url: 'exampleA.php', 
     type: 'GET', 

    success: function(data) { 
//called when successful 
$('#ajaxphp-results').html(data); 
    }, 
     error: function(e) { 
//called when there is an error 
//console.log(e.message); 
    } 
     }); 





    $("#checkB").ajax({ 
     url: 'exampleB.php', 
     type: 'GET', 

    success: function(data) { 
//called when successful 
$('#ajaxphp-results').html(data); 
    }, 
     error: function(e) { 
//called when there is an error 
//console.log(e.message); 
    } 
     }); 

即我可以直接使用id来调用ajax,而无需任何事件

根据用户类型(A,B)有一个登录表单,将通过ajax显示相应的表单(A,B)。这里如果条件satiafied阿贾克斯呈现形式和AJAX渲染formB

能不能做到

这可能吗? 如果不是我如何实现呢?

+0

你能告诉我们 –

+0

你的意思是你的情况可以手动触发回调函数? – MjrKusanagi

+0

@janinahane更新我的问题 – user3419342

回答

0

您可以通过包装内$(function(){...});$(window).load(function() {...});您的AJAX调用调用它在页面加载

$(function() { 
    $.ajax({ 
     url: 'example.php', 
     type: 'GET', 

     success: function (data) { 
      //called when successful 
      $('#ajaxphp-results').html(data); 
     }, 
     error: function (e) { 
      //called when there is an error 
      //console.log(e.message); 
     } 
    }); 
}); 
+0

雅这件事情我知道:)但我的需要已更新我的问题,请看看 – user3419342

相关问题