Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 隐藏和显示信息jquery ajax_Javascript_Html_Jquery_Ajax - Fatal编程技术网

Javascript 隐藏和显示信息jquery ajax

Javascript 隐藏和显示信息jquery ajax,javascript,html,jquery,ajax,Javascript,Html,Jquery,Ajax,我试图在5秒后隐藏消息并显示警报消息 但不起作用 我想在单击“提交”后,在5秒钟后隐藏消息并显示alertMessage <script> $(document).ready(function () { $("#Submit").click(function(event) { Execute(); setTimeout(function(){ \$("#alertMessage").sh

我试图在5秒后隐藏消息并显示警报消息 但不起作用

我想在单击“提交”后,在5秒钟后隐藏消息并显示alertMessage

<script>
  $(document).ready(function () {
    $("#Submit").click(function(event) {
       Execute();
        setTimeout(function(){
        \$("#alertMessage").show();
        \$("#Message").hide();
        }, 500);
    });

    function Execute(){
      $.ajax({
        type: 'POST',
        url: 'url',
        data: {},
         success: function(res) {},
        error: function() {
          alert("failed");
        }
      });
    };

  });
</script>

$(文档).ready(函数(){
$(“#提交”)。单击(功能(事件){
执行();
setTimeout(函数(){
\$(“#alertMessage”).show();
\$(“#消息”).hide();
}, 500);
});
函数Execute(){
$.ajax({
键入:“POST”,
url:'url',
数据:{},
成功:函数(res){},
错误:函数(){
警报(“失败”);
}
});
};
});
HTML


.hideMessage{display:none;}
这是警报信息。
默认文本。

您的
alertMessage
有一个类(
hideMessage
),它在调用
.show()
时将其隐藏-特别是
.show()
只更改元素的
样式,它不知道您的自定义类

class=“hideMessage”
更改为
style='display:none'
以使其与.show()一起显示

这里有两个片段演示了它的工作原理

setTimeout(()=>{
$(“#alertMessage”).removeClass(“隐藏消息”);
$(“#消息”).hide();
}, 1500);
.hideMessage{display:none;}
#alertMessage{color:red;}

这是警报信息。

默认文本它不工作,它只隐藏消息但不显示AlertMessage我添加了两个具有不同解决方案的代码段。如果这些都没有帮助,请更新您的问题,以包括您尝试过的(从上面)不起作用的内容-您可以删除ajax代码,因为这似乎不相关。但它确实隐藏了#消息?重新加载页面没有问题(请参阅我对问题的评论)您的
#submit
的HTML是什么?这是一个

 <style>
    .hideMessage{display:none;}
   </style>
   
   
    <div id="alertMessage" class="hideMessage"> This is alert message. </div> 
    <div id="Message"> default text. </div> 
<div id="alertMessage" style="display:none;"> This is alert message. </div> 
$("#alertMessage").removeClass("hideMessage")