简体   繁体   English

在一个AJAX调用中调用html内部的jQuery函数

[英]Calling a jQuery function inside html return from an AJAX call

I am working on a web page that is using jQuery. 我正在使用jQuery的网页。 I have an Ajax call that gets data from the server and updates a div. 我有一个Ajax调用,从服务器获取数据并更新div。 Inside that data there is a jQuery function, but the function is not being called after the data is loaded into the page. 在该数据中有一个jQuery函数,但在将数据加载到页面后不会调用该函数。 I have the proper js files included in the page already. 我已经在页面中包含了正确的js文件。

This is what is returned from the Ajax call and placed into a div: 这是从Ajax调用返回并放入div的内容:

<script type="text/javascript">
    $(function() {
         $('input').myFunction('param');             
    }); 
</script>
<p> other html </p>

How do I get the returned javascript to run after the html is inserted into the page? 将html插入页面后如何获取返回的javascript?

(I am using Rails with the jRails plugin ) (我正在使用带有jRails插件的Rails)

If you want JavaScript tag evaluation, with html content, you should set the dataType option of the ajax call to "html": 如果你想要使用html内容进行JavaScript标记评估,你应该将ajax调用的dataType选项设置为“html”:

$.ajax({
  type: "GET",
  url: "yourPage.htm",
  dataType: "html"
});

Or dataType "script", if you want to load and execute a .js file: 或者dataType“script”,如果要加载和执行.js文件:

$.ajax({
  type: "GET",
  url: "test.js",
  dataType: "script"
});

more info here: Ajax/jQuery.ajax 更多信息: Ajax / jQuery.ajax

I had a similar problem where I wanted to add little jquery date pickers to a couple fields I was retrieving via ajax. 我有一个类似的问题,我想添加小jquery日期选择器到我通过ajax检索的几个字段。 here is what I did to get around it... just quick and dirty. 这就是我绕过它所做的...只是快速而肮脏。 Instead of returning this whole block from my ajax call: 而不是从我的ajax调用返回整个块:

<script type="text/javascript">
    $(function() {
         $('input').myFunction('param');                         
    }); 
</script>
<p> other html </p>

I would return this (note the made up |x| separator) 我会回复此(注意组成| x |分隔符)

    $(function() {
         $('input').myFunction('param');                         
    }); 

|x|

<p> other html </p>

Then when I received the data back via ajax, I split the return value into 2 parts: the javascript to be executed, and the html to display: 然后当我通过ajax收回数据时,我将返回值分成两部分:要执行的javascript和要显示的html:

 r = returnvalfromajax.split("|x|");       
 document.getElementById('whatever').innerHTML = r[1];  
 eval(r[0]);

Changing 更改

<script type="text/javascript">
...
</script>

To: 至:

<script>
...
</script>

Fixed the issue for me. 解决了我的问题。

Well, you can use jQuery load() function: http://docs.jquery.com/Ajax/load#urldatacallback As a callback you can define function which will be executed after loading new content example: 那么,您可以使用jQuery load()函数: http//docs.jquery.com/Ajax/load#urldatacallback作为回调,您可以定义在加载新内容示例后将执行的函数:

$("#feeds").load("new_content.html",, doSomething()); $(“#feeds”)。load(“new_content.html”,, doSomething());

and in new_content.html you can define function doSomething()... 在new_content.html中你可以定义函数doSomething()...

对于ajax.net, endRequestHandler(sender, args)解决方案工作正常

have you tried Sys.WebForms.PageRequestManager.add_endRequest method? 你试过Sys.WebForms.PageRequestManager.add_endRequest方法吗?

    $(document).ready(function()
    {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);           
    }
    );
    function endRequestHandler(sender, args)
    {
        // whatever
    }

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

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