简体   繁体   English

模式弹出窗口中的“取消”按钮不起作用

[英]cancel button in modal popup not working

I have a view that gets an ajax partial view and shows it as a modal popup. 我有一个获取ajax部分视图并将其显示为模式弹出窗口的视图。 The problem is, the partial view has a cancel button on it with a certain id attribute, but its .click javascript event is attached to the main view. 问题是,局部视图上具有具有特定id属性的取消按钮,但是其.click javascript事件已附加到主视图。 so something like this: 所以像这样:

Main View 主视图

<div>
 some_ajax_link
</div>

<div id="partial_container"></div>

<script>
  $(document).ready(function () {
        $('ajax_link').click(function (e) {
            var hiddenSection = $('#partial_container');
            hiddenSection.fadeIn()
                // unhide section.hidden
                .css({ 'display': 'block' })
                // set to full screen
                .css({ width: $(window).width() + 'px', height: $(window).height() + 'px' })
                .css({
                    top: ($(window).height() - hiddenSection.height()) / 2 + 'px',
                    left: ($(window).width() - hiddenSection.width()) / 2 + 'px'
                })
                // greyed out background
                .css({ 'background-color': 'rgba(0,0,0,0.5)' });
        });
});
$('#partial_container').on('click', '#close_button', function () {
    $('#partial_container').fadeOut();
});
</script>

Partial 部分的

<div> Some stuff </div>
<button id="close_button">Cancel</button>

The reason I have the cancel button in the partial is for styling purposes, when the button is in the main view it works fine for some reason. 我之所以在局部视图中使用“取消”按钮是出于样式目的,当该按钮在主视图中时,出于某种原因它可以正常工作。 I'm confused because when the partial is retrieved it ends up on the same page 我很困惑,因为当检索部分时,它最终出现在同一页面上

EDIT 编辑

not sure why theres .appendTo('body'), I got this script somewhere online, is it necessary? 不知道为什么会有.appendTo('body'),我将此脚本在线放置在某处,是否有必要?

 $('#partial_container').on('click', '#close_button', function () { 
     $('#partial_container').fadeOut(); 
 });

I ended up adding the script on the partial view where the button is 我最终在按钮所在的局部视图上添加了脚本

$('close_button').click(function() {
 $('modal_popup_id').fadeOut();
});

it seems like when the modal 'fades in' things in the background are disabled temporarily, so the click event is not called when the button is clicked (or any script on the background main view is not triggered) 看起来好像是暂时禁用了背景中的模式“淡入”事物,因此单击按钮时(或不触发背景主视图上的任何脚本)都不会调用click事件。

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

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