javascript - 从函数本身内部的按钮调用 jquery 函数

标签 javascript jquery jquery-mobile

这里是调用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>');

关于javascript - 从函数本身内部的按钮调用 jquery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19262426/

相关文章:

javascript - 如何匹配 "something":"thestring"模式?

javascript - 使用 PHP 将 HTML 标签内重复内容中的某些内容替换为 ID

javascript - 模拟 shift-mouseclick

Jquery,.height() 在页面刷新时改变,但在加载时不改变

jquery - 如何改变jquery mobile页脚的背景颜色?

javascript - 引用错误 : $ is not defined jquery mobile

javascript - 如果浏览器选项卡处于事件状态或不活动状态,如何更改 Shopify 标题

javascript - 更改下拉列表中的文本

javascript - 在一页中查看 JavaScript 变量及其值

android - phonegap android ajax over ssl 失败