简体   繁体   English

使用jquery我想在输入为空时隐藏一个div?

[英]using jquery i want the a div to hide when the input is empty?

For example say i have a text box and a hidden div called suggestions 例如,假设我有一个文本框和一个名为advice的隐藏div

$("#suggestinput").on({
  keyup: function(){
    $("#suggestions").addClass("active");
  },
  blur: function () {
    $("#suggestions").removeClass('active');
  }
 //another event to know if input is empty while on focus, then removeClass('active');
});

The first event is when the user types in the input show suggestion, the second one is blur, so when the user unfocuses on the input the suggestions el will hide, i want a third event to check while on focus if the input is empty remove active class. 第一个事件是当用户输入输入节目建议时,第二个事件是模糊的,所以当用户解开输入时建议el将隐藏,我想要在焦点上检查第三个事件,如果输入为空则删除活跃的课程。

Working example is here thanks: http://jsfiddle.net/HSBWt/ 工作示例在此感谢: http//jsfiddle.net/HSBWt/

Try this - http://jsfiddle.net/HSBWt/1/ 试试这个 - http://jsfiddle.net/HSBWt/1/

$("#suggestinput").on({
  keydown: function(){
    $("#suggestions").addClass("active");
  },
  blur: function () {
    $("#suggestions").removeClass('active');
  },
  keyup: function () {
      if ( $(this).val() == "" ) {
        $("#suggestions").removeClass('active');
      }
  }
});

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

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