简体   繁体   English

无法在使用Ajax调用MVC调用的局部视图中运行javascript

[英]Unable to run javascript in partial view called using ajax call MVC

I have this JavaScript code in a partial view that gets the Geo-location of a client. 我在部分视图中具有此JavaScript代码,该视图获取了客户端的地理位置。

<h1>Map</h1>
<style type="text/css">
    #map-canvas {
        height: 500px;
    }
</style>

<script type="text/javascript"
        src="http://www.google.com/jsapi?autoload={'modules':[{name:'maps',version:3,other_params:'sensor=false'}]}"></script>

<script type="text/javascript">        
    function initialize() {

        var myLatlng = new google.maps.LatLng(37.4219985, -122.0839544);
        var mapOptions = {
            zoom: 12,
            center: myLatlng
        }
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'Provider'
        });

    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

<div id="map-canvas"></div>

From another view I am making an Ajax call to load this partial view on an action-link click. 从另一个视图,我正在进行Ajax调用,以在操作链接单击上加载此局部视图。

<script>
    $(function () {
        $('.provider-details').on('click', function (e) {

            $.get("/Provider/Map", function (response) {
                $('#map').html(response)
            });

            e.preventDefault();
        })
    });
</script>

I am able to call the partial view, I can see the text; 我可以调用局部视图,可以看到文本; however, the map is not loading. 但是,地图未加载。 I can see the map if I load the view directly. 如果直接加载视图,我可以看到地图。 Any suggestions? 有什么建议么?

your initialize function is not triggering cause its probably attached to window not the body. 您的初始化函数未触发,可能是因为它可能附加到窗口而不是主体。 just call it after the ajax call succeeds and you're good to go: 只要在ajax调用成功之后调用它,您就可以开始了:

$(function () {
    $('.provider-details').on('click', function (e) {

        $.get("/Provider/Map", function (response) {
            $('#map').html(response);
            initialize();
        });

        e.preventDefault();
    })
});

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

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