简体   繁体   English

javascript单击部分视图后不触发

[英]javascript click not firing after partial view load

I have a view with a javascript section. 我有一个带有javascript部分的视图。 In this view I have javascript with an onclick event which fires on a tab click to load a partial view this all works perfectly. 在此视图中,我具有带有onclick事件的javascript,该事件会在单击选项卡单击时触发,以加载部分视图,这一切都可以正常运行。

Now I know javascript on a partial view wont work properly so in the main view I want to add a click event for when a print button gets clicked. 现在,我知道部分视图上的javascript无法正常工作,因此在主视图中,我想为单击打印按钮时添加click事件。

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

        $('#printBtn').click(function () {
            debugger;
            printElement(document.getElementById("modal-body"));
            window.print();
        });

        $('#tabStrip a').click(function (e) {
            e.preventDefault()
            var tabID = $(this).attr("href").substr(1);

            $(".tab-pane").each(function () {
                $(this).empty();
            });

            $("#" + tabID).empty().append("<div class='loader'><img src='/Content/img/Loader/ajax-loader.gif' alt='Loading' /></div>");

            $.ajax({
                url: "/Account/CustomerTab",
                data: { Id: tabID },
                cache: false,
                type: "get",
                dataType: "html",
                success: function (result) {
                    $("#" + tabID).empty().append("<div id='replaceDiv'>" + result + "</div>");
                    $(document).trigger('ready');
                    debugger;
                }
            });
            $(this).tab('show')
        });
    });
</script>

so the tab click works perfectly but once my partial view is loaded then the print button debugger doesn't get hit. 因此,单击选项卡的效果很好,但是一旦加载了我的局部视图,便不会点击打印按钮调试器。 this is the button in my partial view. 这是我局部视图中的按钮。

<button id="printBtn" type="button" class="btn btn-default">Print</button>

Try changing your clickhandler to something like this: 尝试将Clickhandler更改为以下内容:

$(document).on('click','#printBtn', function() {
  //your code here
  }

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

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