簡體   English   中英

Bootstrap Modal 在條件下打開

[英]Bootstrap Modal open on condition

我正在使用 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>

我的 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 那么我不想打開彈出窗口。 現在彈出內容為空的打開。

問題是您的按鈕上有data-toggle="modal" ,這是使用模式的數據屬性(HTML5)方式。 這將在沒有編寫任何 JavaScript 的情況下工作。

刪除data-toggle然后你的 javascript 應該可以正常運行。

文件

試着做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM