在功能完成任务后隐藏DIV

时间:2015-03-09 22:19:03

标签: javascript jquery html

我写了一段自动回复用户文本输入的代码。 自动"在线聊天"所以说。

虽然当脚本用尽了我想要禁用子状态和按钮的脚本时,我真的不知道如何做这样的事情。

我的代码:

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
    $(document).ready(function() {
      $("#typing").hide();
      var n = "You:<br>";
      var o = $('#outputWindow');
      var i = $('#inputWindow');
      var s = $('#sendButton');
      var t = $('#typing');      
      var r = -1;

      //arrays
      var msg = ['msg1', 'msg2', 'msg3'];

      //fire send events
      $(s).click(function() {
      runAI();
      });
      $(i).keydown(function(e) {
      if (e.keyCode == 13) {
            runAI();
      }
      });

      function runAI() {
      if (i.val().length > 0) {
            r = r + 1;
            o.html(o.html()+n+$("#inputWindow").val()+"<br><hr>" );      
            setTimeout(function(){ $("#typing").show();  }, 3000);            
            setTimeout(function(){ o.html(o.html()+"Username:<br>"+msg[r]+"<br><hr>") }, 7000);
            setTimeout(function(){ $("#typing").hide();  }, 8000);
            i.val('');
            i.focus();
      }
      }
      i.focus();
    });
});//]]>  

</script>

这个想法是在脚本响应之后(在这种情况下)隐藏提交表单和按钮:msg1,msg2和msg3。

如果有人可以提供帮助,那就太棒了!

2 个答案:

答案 0 :(得分:3)

这样就可以了。放在runAI()功能的底部。

每次调用r+1时都会检查runAi()。当它检测到它大于或等于消息数组长度时,它将在发送最后一条消息后隐藏用户输入的可能性。

  function runAI() {
     if (i.val().length > 0) {
        r = r + 1;
        o.html(o.html()+n+$("#inputWindow").val()+"<br><hr>" );      
        setTimeout(function(){ $("#typing").show();  }, 3000);            
        setTimeout(function(){ o.html(o.html()+"Username:<br>"+msg[r]+"<br><hr>") }, 7000);
        setTimeout(function(){ $("#typing").hide();  }, 8000);

        if (r+1 >= msg.length)
        {
           $('#inputWindow').hide();
           $('#sendButton').hide(); 
           return true; // end the function here;
        }
        else
        {
           i.val('');
           i.focus();
        }
     }
  }

r达到大于或等于数组长度的长度时,输入和按钮将被隐藏。

答案 1 :(得分:0)

好吧,只需使用CSS隐藏它:

$(<your object>).css('display', 'none');

将此项用于您要隐藏的每个对象。访问www.w3schools.com并查看display属性的可用值。

相关问题