简体   繁体   English

使用JQuery激活Bootstrap警报

[英]Activate Bootstrap alerts with JQuery

I'm new to JQuery and my problem is that I have a button in a modal such that when I click it, a JQuery script is run. 我是JQuery的新手,我的问题是我在模式中有一个按钮,因此当我单击它时,将运行JQuery脚本。 I need this script to show an alert box. 我需要此脚本来显示警报框。

The code: 编码:

<div id="formAlert" class="alert alert-warning hide">
    Please enter your    
</div>

The above is inside the modal. 上面是模态内部。

    <script type="text/javascript">

        $(document).ready(function (){

            $("#submitMe").click(function(){
                $("#formAlert").show();
            });

        });
    </script>

Thanks for your help! 谢谢你的帮助!

First of all, you'll need to hide the alert using display: none in CSS. 首先,您需要使用display: none隐藏警报display: none CSS中display: none You're achieving that using Bootstrap's .hide class, so that should be sufficient. 您已经使用Bootstrap的.hide类实现了这一点,因此就足够了。

Next, alert() isn't a proper function of a jQuery object. 接下来, alert()不是jQuery对象的适当功能。 It's a function of the window object and is used to display a system alert. 它是window对象的功能,用于显示系统警报。

You just need to use jQuery's show() function. 您只需要使用jQuery的show()函数。 You may also need to prevent the default action of the event (ie the submit button click) using e.preventDefault() . 您可能还需要使用e.preventDefault()阻止事件的默认操作(即,单击提交按钮e.preventDefault() For example, your script should look like this: 例如,您的脚本应如下所示:

$(function() {  
    $('#submitMe').on('click', function(e) { 
        e.preventDefault();
        $('#formAlert').show();
    });
});

Notice that we use the $(function() { }) shortcut for $(document).ready(function() { }); 注意,我们对$(document).ready(function() { });使用$(function() { })快捷方式$(document).ready(function() { }); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM