在Ajax调用之后重新加载jQuery函数

时间:2014-03-28 13:52:17

标签: javascript jquery ajax

我不太了解jQuery,我想知道如何在ajax调用之后重新加载jQuery的所有函数,因为我在a中添加元素然后新元素不起作用但其他工作细

ajax电话:

$(document).ready(function () {

    $('#search-bar').keyup(function (event) {
        if (event.keyCode == 13) {
            myFunction();

        }
    });

        var city = $('#search-bar').val();
        $.ajax({
            url: '@Url.Action("getWeatherSearch", "Index")',
            data: { city: city },
            //async: false,
            error: function (resp) {
                var div = document.getElementById('rb-grid');
                div.innerHTML = resp.responseText + div.innerHTML;
            },
            success: function (resp) {
                $("#rb-grid").prepend(resp);
            }
        });

HTML:

文本框:                                      @ Html.TextBox(“search-bar”,“”,new {@class =“search-bar”,placeholder =“Pesquisar”})                 

这是我想添加新的

  • ,我创建了dinamically,使用ajax从c#调用方法:

            <ul id="rb-grid" class="rb-grid clearfix">
    
                <li class="icon-clima-1 rb-span-2">
                    <h3>@ViewBag.CurrentCity</h3><span class="rb-temp">@ViewBag.CurrentTemp°C</span>
                    <div class="rb-overlay">
                        <span class="rb-close">close</span>
                        <div class="rb-week">
                            <div><span class="rb-city">@ViewBag.CurrentCity</span><span class="icon-clima-1"></span><span style="font-size: 60px !important;">@ViewBag.CurrentTemp ºC</span><span style="font-size: 40px !important;">Min: @Next0.tempMinC ºC</span></div>
                            <div style="width: 17.5%;"><span>@Next1.date.DayOfWeek.ToString().Substring(0, 3)</span><span class="icon-clima-1"></span><span style="font-size: 40px !important;">Max: @Next1.tempMaxC ºC <br /><br /> Min: @Next1.tempMinC ºC</span></div>
                            <div style="width: 17.5%;"><span>@Next2.date.DayOfWeek.ToString().Substring(0, 3)</span><span class="icon-clima-1"></span><span style="font-size: 40px !important;">Max: @Next2.tempMaxC ºC <br /><br /> Min: @Next2.tempMinC ºC</span></div>
                            <div style="width: 17.5%;"><span>@Next3.date.DayOfWeek.ToString().Substring(0, 3)</span><span class="icon-clima-1"></span><span style="font-size: 40px !important;">Max: @Next3.tempMaxC ºC <br /><br /> Min: @Next3.tempMinC ºC</span></div>
                            <div style="width: 17.5%;"><span>@Next4.date.DayOfWeek.ToString().Substring(0, 3)</span><span class="icon-clima-1"></span><span style="font-size: 40px !important;">Max: @Next4.tempMaxC ºC <br /><br /> Min: @Next4.tempMinC ºC</span></div>
                        </div>
                    </div>
                </li>
    

    这就是我激活Boxgrid的方式:

    <script>
        $(function () {
            Boxgrid.init();
        });
    </script>
    
  • 3 个答案:

    答案 0 :(得分:1)

    在ajax回调中再次初始化

     success: function (resp) {
         Boxgrid.init();
     }
    

    答案 1 :(得分:1)

    您需要做的是添加将事件绑定到新添加的DOM元素的能力;您的代码目前仅绑定到当前的DOM元素。

    相反,请使用$.on()执行以下操作:

    $(<selector>).on(<event>, <callback)

    答案 2 :(得分:0)

    您使用的是viewModel吗?如果是这样,您需要在AJAX成功函数后重新绑定viewmodel。你需要先添加一些代码才能进一步帮助你。