【问题标题】:ASP.NET MVC - Losing JS reference of element after reload PartialViewASP.NET MVC - 重新加载 PartialView 后丢失元素的 JS 引用
【发布时间】:2013-07-29 12:09:20
【问题描述】:

我有这种情况:在选择任何国家后重新加载“州”div 内容的国家下拉列表。

@Html.DropDownListFor(m => m.SelectedCountry, 
new SelectList(Model.Countries, "CountryId", "CountryName", Model.SelectedCountry))

<div id="states">
    @Html.Partial("Partial/States", Model)
</div>

<script type="text/javascript">
    $(document).ready(function() {

        $('#SelectedCountry').change(function() {

            if ($(this).val() !== "-1") {

                $.get('@Url.Action("LoadStates", "Controller")', 
                $('#form').serialize(), function(result) {
                    $('#states').html(result);
                });
            }
        }
    });
</script>

好的,它工作得很好。我的问题是当我重新加载这个 PartialView 时,内容丢失了它的 css 样式,我在 JS 引用中找不到它。我有这个功能可以根据选定的下拉值启用按钮。

<script type="text/javascript">
    function canEnableButton() {
        //...
    }

    $('#SelectedCountry, #SelectedState').change(function() {
        canEnableButton();
    }
</script>

change() jQuery 函数工作的第一个加载页面。当我重新加载时,此功能停止工作并且 CSS 样式消失。

有人知道可以是什么吗?谢谢!

【问题讨论】:

    标签: jquery asp.net-mvc partial-views


    【解决方案1】:

    早上好,尝试更改您的事件绑定以使用 jquery .on 绑定:

    $('#SelectedCountry').on('change', function() {
        ...
    });
    
    $('#SelectedCountry, #SelectedState').on('change', function() {
        ...
    });
    

    【讨论】:

      【解决方案2】:

      您可以在 PartialView 中简单地添加 js 和 css 引用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 1970-01-01
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多