Javascript 从html视图调用Get函数
我有返回部分视图的javascript代码Javascript 从html视图调用Get函数,javascript,get,Javascript,Get,我有返回部分视图的javascript代码 var url1=""; $.get(url1, { 'id': Gid }, function (result) { $("#abcd").html(result); }); 返回视图是 <div id="goalReport"> </div> <script type="text/javascript"> function LoadReport() { } </scrip
var url1="";
$.get(url1, { 'id': Gid }, function (result) {
$("#abcd").html(result);
});
返回视图是
<div id="goalReport">
</div>
<script type="text/javascript">
function LoadReport() {
}
</script>
但它不起作用
$.get(url1, { 'id': Gid }, LoadReport);
这是你想要的吗?抱歉,我不完全理解您的问题您在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();
}
);
但是,还有另一种方法,您可以通过一些正则表达式或字符串函数找到脚本内容,并调用的“eval”
函数进行初始化,而无需将html代码附加到文档中。我自己不推荐这种方式,因为这是一个错误的想法,它可能会导致一些愚蠢的例外
希望能有帮助
干杯您想在哪个事件上调用LoadReport?由于get函数,我得到了这个视图。其中包含加载报告功能。您不能这样做。。。。。你在LoadReport()函数中实际做了什么。。??你可以发布Loadreport()代码..不,这个get函数返回这个在html视图中扭曲的Loadreport函数。我想从该视图调用loadreport函数。结果包含loadreport函数。我想从result访问load report函数,我可以访问result.LoadReport吗???我建议您将js脚本移动到单独的文件中,然后添加到某个action$.get(url1,{'id':Gid},LoadReport)的页面上;这段代码也会这样做。干杯,兄弟,我正在考虑另一种方法来调用这个eval($(“脚本”,“$(结果)).text()),但我不确定它是否有效。希望听到。
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();
}
);