2017-10-15 81 views
0

我有一個帶Bootstrap 3的網頁,我試着在下面的代碼中顯示警告框。如何使用外部JavaScript顯示Bootstrap 3警報

<button class="btn btn-warning" onclick="warning('Are you sure ?')">Warning</button> 

它的工作正常,這與下面的Javascript在我的網頁上。

window.error = function(msg) { 
    var dom = '<div class="top-alert"><div class="alert alert-danger alert-dismissible fade in " role="alert"><i class="glyphicon glyphicon-exclamation-sign"></i> ' + msg + '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div></div>'; 
    var jdom = $(dom); 
    jdom.hide(); 
    $("#id_template").append(jdom); 
    jdom.fadeIn(); 
    setTimeout(function() { 
     jdom.fadeOut(function() { 
      jdom.remove(); 
     }); 
    }, 6000); 
} 

對於外部JavaScript文件的警告()函數是給的ReferenceError:警告沒有定義

我的例如

// JavaScript Document 

function fetch(){ 

    var user = $("#id_owner").val(); 
    var firstname = $("#id_fname").val(); 
    if(firstname == ""){ 
     warning('You must enter First Name.'); 
     return false; 
    } 

} 
+0

你能否提供警告腳本 –

+0

我沒有使用任何警告腳本,不知道該怎麼做。 –

+0

嘗試將window.error更改爲window.warning - https://codepen.io/nagasai/pen/GMYQGG –

回答

1

你一定要試試下面的代碼下面給出外部的js直接從你的外部javascript。

if(firstname == ""){ 

    var dom = '<div id="modals" class="alert alert-danger" style="display:none;"><button data-dismiss="alert" class="close" type="button">×</button><span class="entypo-attention"></span> <strong>Waring</strong>&nbsp;&nbsp;You must enter First Name.</div>'; 
    var jdom = $(dom); 
    jdom.hide(); 
    $("id_template").append(jdom); 
    jdom.fadeIn(); 
    setTimeout(function() { 
     jdom.fadeOut(function() { 
      jdom.remove(); 
     }); 
    }, 6000); 
} 
+0

像魅力一樣工作。謝謝! –