简体   繁体   English

调用ajax成功回调函数

[英]Call a function on ajax success call back

Below is my script下面是我的脚本

function allresultscount(thisurl) {
$.ajax({
type: "GET",
dataType: "xml",
url: thisurl,
success: function(data) {
    if ($(data).find('atom\\:entry, entry').length !== 0) {
        $(data).find('atom\\:entry, entry').each(function() {
            countall++;
        });
    }
    $("#resultscount").text(countall);
    var pagecount = countall / 2;
    for (var i = 1; i <= pagecount; i++) {
        $('<a class="page_link" onclick="formurl(' + i + ')" longdesc="0" style="display: inline-block;">' + i + '</a>').insertBefore('.next_link');
    }
}
});
}
function formurl(i) {
   alert(i);
}

Here am trying to call a function formurl() in ajax success callback.这里我试图在 ajax 成功回调中调用函数 formurl()。 Both functions are in document ready.这两个功能都已准备就绪。

But am getting formurl() undefined error.但是我收到了 formurl() 未定义的错误。 How cal I do this?我怎么能这样做?

You can but they must be called within the scope of the ready() method otherwise they lose scope when the ready() method exits.您可以,但必须在 ready() 方法的范围内调用它们,否则当 ready() 方法退出时它们将失去作用域。

For example, the code below will work:例如,下面的代码将起作用:

$(document).ready(function(){
  function formurl()
  {
    alert('Bar');
  }

  formurl(); // still in the scope of the ready method
});

After you ajax is complete, your new elements are rendered in DOM. ajax 完成后,您的新元素将在 DOM 中呈现。 Now you are outside the ready() event.现在您在 ready() 事件之外。 In your case, you could define your function outside ready() event在您的情况下,您可以在 ready() 事件之外定义您的函数

function formurl()
      {
        alert('Bar');
      }
$(document).ready(function(){

     // fire ajax here
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM