浮动标签输入未注册文本

时间:2016-05-09 17:38:37

标签: javascript css labels floating

这很奇怪。今天早些时候,我发布了一个包含浮动标签的表单的代码。问题是代码中的JS如果光标或文本被放入输入中就会注册,因为无论你放在那里,标签都会浮动下来。

当我发布代码时,一些人说它在他们的结尾工作正常,甚至将代码复制回jsfiddle和他们的版本工作。我把他们的代码版本工作并将其复制到一个空白的js小提琴中来比较代码,它立即停止了工作。

任何人都可以帮助我。



join, invokeAll, CountDownLatch

$(function() {
  $(".formStyle7 .input-group input").focusout(function() {

    var text_val = $(this).val();
    if (text_val === "") {
      $(this).removeClass('has-value');
    } else {
      $(this).addClass('has-value');
    }

  });

});

.formStyle7 {
  display: block;
  height: 750px;
  width: 500px;
  background-color: #FFFFFF;
  border: none;
  border-radius: 3px;
  box-shadow: rgb(128, 128, 128) .1px .1px 5px, rgb(128, 128, 128) -.1px -.1px 5px;
  margin: 30px auto;
}
input {
  background: none;
  border: 1px solid #21a1e1;
  margin: 15px;
  display: inline-block;
  height: 30px;
  width: 455px;
}
input:focus,
input:active {
  outline: none;
}
input[type="text"],
input[type="email"] {
  border: none;
  border-bottom: 1px solid #b3c1cc;
}
.input-group {
  position: relative;
}
.input-group label {
  position: absolute;
  left: 15px;
  top: 30px;
  font-style: italic;
  font-size: 18px;
  color: #999;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  pointer-events: none;
  transition: all 0.2s ease;
}
.input-group input:focus + label,
.input-group input.has-value + label {
  top: 13px;
  font-size: 12px;
  color: #aaa;
}
#proposedWorkshopDDL {
  background: none;
  border: 1px solid #21a1e1;
  margin: 15px;
  display: inline-block;
  height: 30px;
  width: 455px;
}
#unitPresidentDDL {
  background: none;
  border: 1px solid #21a1e1;
  margin: 15px;
  display: inline-block;
  height: 30px;
  width: 455px;
}
.clearFix {
  clear: both;
}
header {
  height: 40px;
  width: 500px;
  font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida         Sans', Arial, sans-serif;
  font-size: 24px;
  color: #b3c1cc;
  float: left;
  margin: 25px 10px 0px 15px;
}
p {
  height: 40px;
  width: 500px;
  font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
  font-size: 20px;
  color: #b3c1cc;
  float: left;
  margin-left: 15px;
  margin-top: -5px;
}
.formContainer {
  margin-top: 0px;
}




2 个答案:

答案 0 :(得分:1)

我刚刚测试了您的代码,它运行正常:https://jsfiddle.net/jb013rqe/3/

但看起来您忘记将jQuery包含为外部依赖:

enter image description here

只需将jquery cdn url添加到External Resources部分,例如:

https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js

答案 1 :(得分:0)

始终确保您在页面上加载了jquery。

它对我来说似乎很好。如果文本在输入中,则输入中的占位符会缩小并保持不变。试着把你的js放在这个

$(document).ready(function () {
   /*Code here*/
});

如果这不起作用是否有任何控制台错误?