我有javascript代码,返回部分视图。
var url1="";
$.get(url1, { 'id': Gid }, function (result) {
$("#abcd").html(result);
});
查看返回
<div id="goalReport">
</div>
<script type="text/javascript">
function LoadReport() {
}
</script>
我想在我写的get函数成功时调用load report函数。
如何调用加载报告功能而不是将其附加到div abcd中 我试过像
result.LoadReport
但它不起作用
答案 0 :(得分:0)
$.get(url1, { 'id': Gid }, LoadReport);
这是你想要的吗?对不起,我不完全理解你的问题
答案 1 :(得分:0)
您在javascript中定义的所有内容都在相同的上下文中。它不像C#或其他一些语言,尽管你有范围定义,甚至你可以拥有命名空间。所以您只需将代码更改为:
var url1="";
$.get(url1, { 'id': Gid }, function (result) {
//If the result contains the HTML code you mentioned you should append it to the document
//and browser will initialize the script tag and everything inside it (So you MUST append the script tag to be initiated). So you can call the
//function whenever the script tag is loaded. Because you didn't use the src attribute to
// reference a javascript file, I think you can call the function on the next line, after
// appending the element.
$(result).appendTo(document.body);
LoadReport();
}
);
然而,还有另一种方法可以通过一些RegEx或字符串函数找到脚本内容并调用"eval"
函数并初始化它而不将html代码附加到文档中。我自己不推荐这种方式,因为这是一个错误的想法,它可能会导致你一些愚蠢的例外。
希望它有所帮助 干杯