2013-10-09 24 views
2

這裏裏面的代碼來調用Web服務返回JSON呼叫從一個按鈕一個jQuery函數,該函數本身

$('#page_job_list_pages').live('pageshow',function(){ 

     try { 
     $.ajax({ 
      url: "http://domain.com/json/" + encodeURIComponent(tid), 
      type: 'get', 
      dataType: 'json', 
      error: function (XMLHttpRequest, textStatus, errorThrown) { 
      alert('page_job_list_pages - failed to retrieve pages'); 
      console.log(JSON.stringify(XMLHttpRequest)); 
      console.log(JSON.stringify(textStatus)); 
      console.log(JSON.stringify(errorThrown)); 
      }, 
      success: function (data) { 
      $("#page_job_list_pages_list").html(""); 
      $.each(data.nodes,function (node_index,node_value) { 
       console.log(JSON.stringify(node_value)); 
       if(node_index != 0) { 
        var companyName = node_value.node.field_basic_info.match("Company:(.*)date"); 
        $("#page_job_list_pages_list").append($("<li></li>",{"html":"<a href='#node_view' id='" + node_value.node.Nid + "' class='page_job_list_pages_list_title'>" + companyName[1] + "</a>"})); 
       } 
      }); 
      $("#page_job_list_pages_list").listview("destroy").listview(); 
      $("#page_job_list_pages_list").append('<a onclick="()" data-role="button" data-theme="a">TEST</a>'); 
      } 
     }); 
     } 
     catch (error) { alert("page_job_list_pages_list - " + error); } 
    }); 


this line is a button 

    $("#page_job_list_pages_list").append('<a onclick="()" data-role="button" data-theme="a">TEST</a>'); 

我想打電話給jQuery函數再次查詢JSON。

如何做到這一點?

+0

你想要它現在做什麼?我不確定你的問題是否符合描述。你想在'pageshow'上運行後再次運行該方法嗎? –

+0

點擊按鈕後再次運行它 – hkguile

+0

查看下面我添加的代碼。 –

回答

0

我把你的查詢包裝在一個函數中。我假設這是你想要的。我還在您的按鈕的點擊處理程序中添加了該調用以再次查詢。

注意: 從jQuery 1.7開始,不推薦使用.live()方法。使用.on()附加事件處理程序。老版本的jQuery用戶應優先使用.delegate(),而不要使用.live()。 (來源:http://api.jquery.com/live/

$('#page_job_list_pages').live('pageshow',function(){ 
    queryJSON(); 
}); 

function queryJSON(){ 
    try { 
    $.ajax({ 
     url: "http://domain.com/json/" + encodeURIComponent(tid), 
     type: 'get', 
     dataType: 'json', 
     error: function (XMLHttpRequest, textStatus, errorThrown) { 
     alert('page_job_list_pages - failed to retrieve pages'); 
     console.log(JSON.stringify(XMLHttpRequest)); 
     console.log(JSON.stringify(textStatus)); 
     console.log(JSON.stringify(errorThrown)); 
     }, 
     success: function (data) { 
     $("#page_job_list_pages_list").html(""); 
     $.each(data.nodes,function (node_index,node_value) { 
      console.log(JSON.stringify(node_value)); 
      if(node_index != 0) { 
       var companyName = node_value.node.field_basic_info.match("Company:(.*)date"); 
       $("#page_job_list_pages_list").append($("<li></li>",{"html":"<a href='#node_view' id='" + node_value.node.Nid + "' class='page_job_list_pages_list_title'>" + companyName[1] + "</a>"})); 
      } 
     }); 
     $("#page_job_list_pages_list").listview("destroy").listview(); 
     $("#page_job_list_pages_list").append('<a onclick="queryJSON();" data-role="button" data-theme="a">TEST</a>'); 
     } 
    }); 
    } 
    catch (error) { alert("page_job_list_pages_list - " + error); } 
} 
this line is a button 

$("#page_job_list_pages_list").append('<a onclick="queryJSON();" data-role="button" data-theme="a">TEST</a>');