启动模态在条件下打开

Bootstrap Modal open on condition

本文关键字:条件下 模态 启动      更新时间:2023-09-26

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

<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>

我的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");
            });
        }
    });

如果filters.length<=0,那么我不想打开popup。

问题是你的按钮上有data-toggle="modal",这是使用模态的数据属性(HTML5)方式。

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

文档

尝试:

if (filters.length <= 0) {
    $("#myModal").modal("hide");
}
//insert this code in your condition.
//for example.
    
if(...){
    $('#myModal').modal('show');
 }