2016-11-27 47 views
0

我是一名Javascript初學者,當點擊我的頁面上的menu2以返回一條消息時,我需要爲用戶生成一條警告,爲此我選擇使用與所有瀏覽器兼容的模式插件Plugin modal下面的代碼是我的可以得到直到現在,但沒有成功。 這裏是我的html如何使用sweetalert創建模態警報?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 
 

 
<ul id='menu'> 
 
    <li id='menu1'><a href='/web/front/helpdesk.public.php' title="Home" class='itemP'>Home</a> 
 
    </li> 
 
    <li id='menu2'><a href='/web/front/helpdesk.public.php' title="Cria um chamado" class='itemP'>Cria um chamado</a> 
 
    </li> 
 
    <li id='menu3'><a href='/web/front/ticket.php' title="Chamados" class='itemP'>Chamados</a> 
 
    </li> 
 
    <li id='menu4'><a href='/web/front/reservationitem.php' title="Reservas" class='itemP'>Reservas</a> 
 
    </li> 
 
    <li id='menu5'><a href='/web/front/helpdesk.faq.php' title="FAQ" class='itemP'>FAQ</a> 
 
    </li> 
 
</ul>

下面是我的javascript

echo '<script type="text/javascript">'; 
 
    echo'setTimeout(function() { 
 
    swal 
 
    ("Return the alert test !", 
 
    "Message!", 
 
    "success");'; 
 
    echo '}, 1000); 
 
    </script>';

警報的網頁上正常的迴歸。

enter image description here

我已經嘗試過這種方式,但沒有成功爲止。

 echo '<script type="text/javascript">'; 
 
    echo'setTimeout(function() { 
 
    echo '$(document).ready(function() { 
 
    echo '$('#menu2').click(function(event) { 
 
    swal 
 
    ("Return the alert test !", 
 
    "Message!", 
 
    "success");'; 
 
    echo '}, 1000); 
 
    </script>';

錯誤試圖運行在我的PHP的解決方案。

enter image description here

我不希望被調用的方法做的,事件的默認操作將不會觸發。要做到這一點使用event.preventDefault();但它不適合我。警報沒有出現。有沒有人有辦法解決嗎?

echo '<script type="text/javascript">'; 
 
echo '$(document).ready(function(){'; 
 
echo ' $("#menu2").click(function(event){'; 
 
echo '   event.preventDefault()';   
 
echo '  swal("Return the alert test !", "Message!", "success")'; 
 
echo ' })'; 
 
echo '});'; 
 
echo '</script>';

+0

一個人沒有用的document.ready需要的setTimeout()()。還缺少多個結束括號。檢查你的控制檯。 – Lain

回答

1

你生的jQuery應該是這樣的(正常閉合功能):

$(document).ready(function(){ 
    $('#menu2').click(function(event){ 
     swal("Return the alert test !", "Message!") 
    }) 
}); 

但是你呼應它,是你的。 另外,您不需要通過使用document.ready()來設置setTimeout()。

編輯:

echo '<script type="text/javascript">'; 
echo '$(document).ready(function(){'; 
echo ' $("#menu2").click(function(event){'; 
echo '  swal("Return the alert test !", "Message!")'; 
echo ' })'; 
echo '});'; 
echo '</script>'; 
+0

我在NetBeans中測試過,在php中給語法錯誤並沒有爲我工作。 echo''; –

+0

可能是因爲菜單2的單引號引起的。一個男人不應該混淆不同的語言字符串分隔符。 – Lain

+0

我在這裏測試並沒有爲我解決它,到目前爲止,我只能使用我的示例返回頁面上的警報。什麼最適合你更好地理解? –

0

我能夠按照這裏獲得的答覆,使Alert模式sweetalert工作。我對Javascript有了更好的理解,並且在具體的event.preventdefault中得到了對我來說完美工作的代碼。

?> 
 
<script type="text/javascript"> 
 
    $(document).ready(function() { 
 
    $("#menu2").click(function(event) { 
 
     event.preventDefault(); 
 
     swal("Many thanks to everyone for making me a better programmer.!", "Message", "success"); 
 
    }) 
 
    }); 
 
</script> 
 
<?php