jQuery检测输入是否已填充

jQuery检测输入是否已填充,jquery,if-statement,input,Jquery,If Statement,Input,我创建了这个函数,它可以在输入有焦点时为标签设置动画 $("input").on("focus", function () { $(this).siblings().animate({ top: "-20", }, 70, function() { }); }); 我想动画它回来,如果输入保持未填充 这是我的密码笔 $("input").on("focus", function () { $(this).siblings().animate({ top: "-2

我创建了这个函数,它可以在输入有焦点时为标签设置动画

$("input").on("focus", function () {
  $(this).siblings().animate({
    top: "-20",
  }, 70, function() {
  });
});
我想动画它回来,如果输入保持未填充

这是我的密码笔

$("input").on("focus", function () {
  $(this).siblings().animate({
    top: "-20",
  }, 70, function() {
  });
});

$("input").on("blur", function() {
  if( $(this).val().length == 0 ) {
    $(this).siblings().animate( {
      top : "0"
    });
  }
});
我建议:

$("input").on({
  // using an Object with event-names as the
  // keys, linking to the functions
  // to handle those events:
  'focus': function() {
    $(this).siblings().animate({
      'top': '-20',
    }, 70);
  },
  // handling the 'blur' event on the input:
  'blur': function() {
    // if the value of the <input> element, with
    // leading and trailing white-space removed,
    // is equal to the default value of the <input>
    // (the value it held on page-load)
    // we animate the 'top' CSS property back to 0:
    if (this.value.trim() == this.defaultValue) {
      $(this).siblings().animate({
        'top': 0
      }, 70);
    }
  }
});
div{
边缘:1米;
填充:0;
边界半径:1米;
位置:相对位置;
}
标签{
位置:绝对位置;
排名:0;
}

输入1
输入2
投入3
输入4
输入5

我会使用
focusout
事件,相反,当输入失去焦点时

代码如下:

$("input").on("focus", function () {
  $(this).siblings().animate({
    top: "-20",
  }, 70);
}).on("focusout", function () {
  if( $(this).val().length == 0 ) {
      $(this).siblings().animate({
         top: "0",
      }, 70);
  }
});
当一个元素(或其中的任何元素)失去焦点时,事件发生

$("input").on("focusout", function() {
  if( $(this).val().length == 0 ) {
    $(this).siblings().animate( {
      top : "0"
    });
  }
});

希望它也能有所帮助:)

这是我下面答案的副本。我不是有意复制或复制我已经分享了我所知道的一切。如果你在我之前给出了答案,这不应该是重复的?听起来对吗?大卫·托马斯先生,您好,您在帖子中询问了“您尝试了什么,出了什么问题”,这是初学者常见的问题。。从那里你就知道那个家伙会有什么评论。我和他讨论过,澄清了他的错误,让他承认。请看这家伙邀请的评论“让我们在聊天中继续讨论”。我让他承认错误。你可能不在乎&已经忘记了,但我觉得他不尊重你。