twitter-bootstrap Bootstrap Modal 在条件下打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22397136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 20:17:48  来源:igfitidea点击:

Bootstrap Modal open on condition

javascriptjquerytwitter-bootstrap

提问by Sathish

I'm using Bootstrap modal popup to show content in popup and I'm using if/else condition to open modal popup. I don't want to open modal popup when condition is false. My code:

我正在使用 Bootstrap 模式弹出窗口来显示弹出窗口中的内容,我正在使用 if/else 条件打开模式弹出窗口。当条件为假时,我不想打开模态弹出窗口。我的代码:

<a data-toggle="modal" class="btn btn-primary" style="font-size: 10px" href="#" data-target="#myModal" title="Edit"><span class="glyphicon glyphicon-pencil"></span>Edit</a>

My jQuery is:

我的 jQuery 是:

 $('a[data-target=#myModal]').on('click', function (ev) {
        ev.preventDefault();
        if (filters.length <= 0) {
            alert('Please select any one item in grid');
        }
        else {
            $(this).attr('href', '/GeoRegion/Edit/' + filters[0]);
            var target = $(this).attr("href");

            // load the url and show modal on success
            $("#myModal").load(target, function () {
                $("#myModal").modal("show");
            });
        }
    });

If filters.length<=0 then I don't want to open popup. Now popup opening with empty content.

如果filters.length<=0 那么我不想打开弹出窗口。现在弹出空内容。

回答by Andrew Brown

The problem is that you have data-toggle="modal"on your button, which is the data-attributes (HTML5) way of using modals. this will work without any javascript written.

问题在于您data-toggle="modal"的按钮上有按钮,这是使用模态的数据属性 (HTML5) 方式。这将在没有编写任何 javascript 的情况下工作。

remove data-toggleand then your javascript should run correctly.

删除data-toggle,然后您的 javascript 应该可以正常运行。

documentation

文件

回答by Felix

Try to do:

试着做:

if (filters.length <= 0) {
    $("#myModal").modal("hide");
}