【问题标题】:How to close the modal dialog box in MVC View?如何关闭 MVC 视图中的模态对话框?
【发布时间】:2014-05-06 05:30:21
【问题描述】:

我正在开发 ASP.NET MVC 应用程序。

我已经使用了 boostrap。

当用户单击名为“显示库存”的链接时,我显示了一个模式弹出对话框 (您可以在图像中看到它。)

我在框中显示了确定和取消按钮。

问题是,当我单击取消按钮时,它会关闭,但仅在第一次。 如果我再次打开对话框并单击取消按钮框消失并且纯黑屏出现而不是父屏幕。

我只想关闭模态框。没有提及取消按钮的 URL。 那可能吗 ?

这是我的代码...

  <script type="text/javascript">

    $(document).ready(function () {



        $('#closeA').live('click', function(){
            alert("asd");
            $("body").remove( "div.modal-backdrop  in");

        });



        $('#lnkAddProduct').click(function () {

            // alert("Infuction");

            var rIndex = $("select.clsProductId").length;


            var ndate =  new Date();

            var time = ndate.getMilliseconds();

            var IDD = rIndex + time;



              $('#ProductList').append("<div  class='span12' style='margin-left:0px' ><div class='span3'><select class='clsProductId '  name='ProductId' id='ddProductList_"+IDD+"' style = 'font-size:12px;width:200px;margin-right:10px;margin-left:20px;' onchange='get("+IDD+")'/> </div><div id='ProductCode_"+IDD+"' class='span1'  style=' margin-left:30px;'></div><div id='Weight_"+IDD+"' class='span1' style=' margin-left:90px;'> </div><input type='text' id='Quantity_"+IDD+"' class='clsQuantity'  name='Quantities' style='width:50px; margin-left:35px;' onblur='StockLinkVisible("+IDD+");' /> <a href='#' style='font-size:14px;text-decoration:none;font-weight:bold; color :#ee8929; margin-left:20px;' id='lnkRemove_"+IDD+"' class='clsRemove'  onclick='removeElement(" + IDD+");'>X</a><a href='#' style='font-size:14px;text-decoration:none;font-weight:bold; color:White; margin-left:20px' id='lnkStockInfo_"+IDD+"' class='clsStockInfo'  data-container='body' data-toggle='popover' data-placement='left' data-content='Vivamus sagittis lacus vel augue laoreet rutrum faucibus.' onclick='ShowStockInformation("+IDD+");'>Check Stock</a></div>");


            getProductList(IDD);


        });



    });







    function StockLinkVisible(cnt) {
        $('#lnkStockInfo_'+ cnt).show();
        $('#lnkStockInfo_'+ cnt).css("color", "#ee8929");

    }

    function DisplayAmendAdvancedPaidAmountAlert()
    {


        $('div').remove('#dataConfirmModal');

        var href = $(this).attr('href');
        if (!$('#dataConfirmModal').length) {

            $('body').append('<div id="dataConfirmModal" class="modal" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true">  <div class="modal-header">        <button type="button" class="close" data-dismiss="modal" aria-hidden="true" id="btnClose1"> × </button>        <h5 id="dataConfirmLabel">Stock Availability at different locations.</h5> </div><div class="modal-body" >    <table style="width:530px"><tr> <th style="width:120px">Bhivandi </th><th>Worli</th><th>Santacruze</th> <th> Saki-Naka</th> </tr>    <tr> <td><div >60 </div></td>         <td><div>3</div></td>           <td><div >60 </div></td>         <td><div>3</div></td> </tr>             </table> </div> <div class="modal-footer">       <button type="button" id="btnOk1"  class="btn btn-primary" data-dismiss="modal" aria-hidden="true" >OK</button> <button type="button" id="closeA"  class="btn btn-default" data-dismiss="modal" aria-hidden="true" >Cancel</button> </div></div> ');   
            }       
            $('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
            $('#dataConfirmOK').attr('href', href);
            $('#dataConfirmModal').modal({show:true});


    }




</script>

这是图片...

当我打开对话框时,每次添加带有“modal-backdrop in”类的 Div 并且我无法删除它...我已经在 Google chrome 的 Inspect Element 窗口中检查了这个...

查看下图...

【问题讨论】:

    标签: javascript jquery asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    尝试使用以下方法:

    $('#dataConfirmModal').dialog('close'); 
    

    在点击取消时尝试使用以下内容:

     $('#dataConfirmModal').remove();
    

    $('#dataConfirmModal').parent().remove();
    

    【讨论】:

    • $('#dataConfirmModal').dialog('close');
    • 不...它不工作...它根本没有删除...如果我点击第二次然后我必须在黑屏上点击两次...如果我打开对话框第三次关闭它...然后我必须点击黑屏三次...转到父窗口...等等...这意味着...它没有完全删除它...
    • $("#dataConfirmModal").dialog("destroy");
    【解决方案2】:

    您应该仔细阅读模态文档。

    如果提供了远程 url,内容将通过 jQuery 的 load 加载 方法并注入到 .modal-body

    编辑部分:

    试试这个:

    <script type="text/javascript">
     function DisplayAmendAdvancedPaidAmountAlert()
        {
           $( "div.modal-backdrop.in" ).remove();
           $('div').remove('#dataConfirmModal');
           // Cal();
    
            var href = $(this).attr('href');
            if (!$('#dataConfirmModal').length) {
                $('body').append('<div id= "container" ><div id="dataConfirmModal" class="modal" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true" style="width: 500px;"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h6 id="dataConfirmLabel">Stock Availibility at diffrent locations</h6></div><div class="modal-body "><h4>Stocks</h4><br/>     <table>        <thead>          <tr>              <td>Bhivandi</td>              <td>Santacruze</td>              <td>Worli</td>        </tr>        </thead>        <tbody>            <tr>                <td>12</td>                <td>122</td>                <td>54</td>            </tr>        </tbody>         </table></div>     <div class="alert alert-info" style="display:none;margin:5px" id="alertMessage"> </div><div class="modal-footer"><button id="closeA" class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button><a class="btn btn-primary" id="dataConfirmOK">OK</a></div></div></div> ');      
    
                }       
                $('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
                $('#dataConfirmOK').attr('href', href);
               $('#dataConfirmModal').modal({show:true});
    
    
    
                }
    $(document).ready(function(){
         $('#closeA').live('click', function(){
            $('#container').hide();
         });
    });
    </script>
    

    【讨论】:

    • 不,不工作...当我第二次打开并尝试关闭它时...它再次显示黑屏...我必须双击黑屏然后才能看到父窗口...
    • 不...它不起作用...它根本没有删除...如果我第二次点击我必须在黑屏上点击两次...如果我打开对话框第三次关闭它...然后我必须点击黑屏三次...转到父窗口...
    • 另一种选择是您可以采用另一个 div 并在单击 closeA 按钮时隐藏该 div。如果您的要求仍然不完整,请告诉我,我将编辑我的答案并提供另一个解决方案。
    • 如果您提供其他解决方案会很好...正如我所提到的...如果我保持打开并关闭对话框...我必须单击黑色的次数屏幕,同样的号码。我打开对话框的次数...就像我第 5 次打开对话框并单击取消按钮然后对话框消失并出现黑屏...然后我必须单击该黑屏 5 次才能继续父窗口...
    • 请在 google chrome 中检查您的浏览器控制台,或者如果您使用的是 firefox,则使用 firebug 控制台窗口可能会出现错误。
    【解决方案3】:

    您应该在取消按钮上使用 dismiss="modal" 作为属性或尝试以下操作:

    $("#yourHideCloseButton").on('click', function(){ $(this).closest(".modal").modal('hide'); });

    试试吧,它应该关闭模式,不要尝试删除/删除实际的div

    【讨论】:

    • 试过了,但没有运气... $('#closeA').live('click', function(){ $(this).closest(".modal").modal( '隐藏'); });
    • 您可以为您的页面创建一个 JSFiddle 示例吗?此代码应该在正常和动态的情况下工作。 + 避免使用 .live()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2020-06-20
    • 2011-08-01
    相关资源
    最近更新 更多