2016-11-27 46 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