从ajax响应内容调用函数

时间:2021-09-19 03:19:56

I have a function when the page is ready.

当页面准备好时,我有一个函数。

<script> 
$(function(){
  function do_ajax(){
  do some stuff
  }
});....

This works great on that page. Then I make a .post call and load the response into a div. The response has a

这一页写得很好。然后我进行.post调用并将响应加载到div中

<script>$(selector).click(function(){ do_ajax(); });</script>

with the result. I get an error "do_ajax() is undefined". How can I call do_ajax() from an event from the ajax content?

与结果。我得到一个错误“do_ajax()未定义”。如何从ajax内容中的事件调用do_ajax() ?

Another one.

另一个。

I am using jquery ui datepicker. So, when the page is loaded, I invoke the

我正在使用jquery ui datepicker。所以,当页面被加载时,我调用

$(".datepicker").datepicker();

This works great on that page. Then I make a .post call and load the response into a div. The response has a couple of .datepicker fields. The problem is that the newly loaded datepicker fields don't get the calendar unless I return

这一页写得很好。然后我进行.post调用并将响应加载到div中。响应有两个.datepicker字段。问题是,除非我返回,否则新加载的datepicker字段不会获得日历

<script>$(".datepicker").datepicker();</script>

with the result. Is that how you guys do it or is there another way?

与结果。你们是这么做的,还是有别的办法?

Thanks!

谢谢!

3 个解决方案

#1


3  

When you create functions/variables within the scope of a function, you can't access them from outside (unless you defined them in the global namespace).

当您在函数范围内创建函数/变量时,您不能从外部访问它们(除非您在全局名称空间中定义它们)。

$(function(){
    // I'm inside a function

    var a = function() {...} // Defined locally

    b  = function() {...}    // Defined globally
});

// I can't call a()

// I am able to call b() because it is defined in global namespace.

EDIT:

编辑:

Also, keep in mind that code inside:

同时,记住里面的代码:

$(function() {
     ...
     b  = function() {...}    // Defined globally
});

b(); // Fails, because the document hasn't fully loaded, so 'b' has not been initialized

...waits to run until the document is loaded. So code outside that needs to access globals defined inside will likely execute before the globally defined code is initialized.

…等待运行,直到加载文档。因此,需要访问内部定义的全局变量的外部代码很可能在全局定义的代码初始化之前执行。

#2


0  

The problem is scope related. Your do_ajax function is declared inside the scope of the function run when the page is loaded - thus you can't just arbitrarily call it elsewhere on the page.

问题与范围有关。do_ajax函数在加载页面时运行的函数范围内声明——因此不能在页面的其他地方随意调用它。

There a few ways you could attempt to solve this, the easiest being move the do_ajax function definition outside of your onLoad code, but that also pollutes the global namespace and is generally less desirable.

有几种方法可以尝试解决这个问题,最简单的方法是将do_ajax函数定义移到onLoad代码之外,但这也会污染全局名称空间,而且通常不太理想。

A better approach is during you onLoad code, bind the click method on your selector to your function that calls do_ajax(). It will create a closure around it, and subsequent click events will know how to fire correctly.

更好的方法是在onLoad代码期间,将选择器上的click方法绑定到调用do_ajax()的函数。它将围绕它创建一个闭包,随后的单击事件将知道如何正确地启动。

$(function(){
  function do_ajax(){
    // do some stuff
  }
  // bind the click handler
  $(selector).click(function(){ do_ajax(); });
});

then later...

后来……

$(selector).click(); // fire the click event, which will call do_ajax()

Also note, you probably should return either true or false from your click handler function, as that will control bubbling of the event.

还要注意,您可能应该从单击处理程序函数中返回true或false,因为这会控制事件的冒泡。

I am not familiar enough with the datepicker control. You should also really separate your questions out.

我对datepicker控件不够熟悉。你也应该把你的问题分开。

#3


0  

I think your looking for the Ready() method in the jQuery library.

我认为您正在jQuery库中寻找Ready()方法。

Change:

变化:

$(selector).click(function(){ do_ajax(); });

To:

:

$(document).ready(function(){$(selector).click(function(){ do_ajax(); }}));

This will only run your event when the document object model is fully loaded.

这将只在文档对象模型完全加载时运行事件。

#1


3  

When you create functions/variables within the scope of a function, you can't access them from outside (unless you defined them in the global namespace).

当您在函数范围内创建函数/变量时,您不能从外部访问它们(除非您在全局名称空间中定义它们)。

$(function(){
    // I'm inside a function

    var a = function() {...} // Defined locally

    b  = function() {...}    // Defined globally
});

// I can't call a()

// I am able to call b() because it is defined in global namespace.

EDIT:

编辑:

Also, keep in mind that code inside:

同时,记住里面的代码:

$(function() {
     ...
     b  = function() {...}    // Defined globally
});

b(); // Fails, because the document hasn't fully loaded, so 'b' has not been initialized

...waits to run until the document is loaded. So code outside that needs to access globals defined inside will likely execute before the globally defined code is initialized.

…等待运行,直到加载文档。因此,需要访问内部定义的全局变量的外部代码很可能在全局定义的代码初始化之前执行。

#2


0  

The problem is scope related. Your do_ajax function is declared inside the scope of the function run when the page is loaded - thus you can't just arbitrarily call it elsewhere on the page.

问题与范围有关。do_ajax函数在加载页面时运行的函数范围内声明——因此不能在页面的其他地方随意调用它。

There a few ways you could attempt to solve this, the easiest being move the do_ajax function definition outside of your onLoad code, but that also pollutes the global namespace and is generally less desirable.

有几种方法可以尝试解决这个问题,最简单的方法是将do_ajax函数定义移到onLoad代码之外,但这也会污染全局名称空间,而且通常不太理想。

A better approach is during you onLoad code, bind the click method on your selector to your function that calls do_ajax(). It will create a closure around it, and subsequent click events will know how to fire correctly.

更好的方法是在onLoad代码期间,将选择器上的click方法绑定到调用do_ajax()的函数。它将围绕它创建一个闭包,随后的单击事件将知道如何正确地启动。

$(function(){
  function do_ajax(){
    // do some stuff
  }
  // bind the click handler
  $(selector).click(function(){ do_ajax(); });
});

then later...

后来……

$(selector).click(); // fire the click event, which will call do_ajax()

Also note, you probably should return either true or false from your click handler function, as that will control bubbling of the event.

还要注意,您可能应该从单击处理程序函数中返回true或false,因为这会控制事件的冒泡。

I am not familiar enough with the datepicker control. You should also really separate your questions out.

我对datepicker控件不够熟悉。你也应该把你的问题分开。

#3


0  

I think your looking for the Ready() method in the jQuery library.

我认为您正在jQuery库中寻找Ready()方法。

Change:

变化:

$(selector).click(function(){ do_ajax(); });

To:

:

$(document).ready(function(){$(selector).click(function(){ do_ajax(); }}));

This will only run your event when the document object model is fully loaded.

这将只在文档对象模型完全加载时运行事件。