从函数本身内部的按钮调用 jQuery 函数

calling a jquery function from a button which inside the function itself

本文关键字:函数 按钮 调用 jQuery 内部      更新时间:2023-09-26

这是调用 webservice 以返回 json 的代码

$('#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。

怎么做?

我已经将您的查询包装在一个函数中。我假设这是你想要的。我还在按钮的单击处理程序中添加了调用以再次查询。

注意:从 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>');