【问题标题】:Check if an input is filled or not检查输入是否已填充
【发布时间】:2018-01-29 18:14:41
【问题描述】:

我是 Javascript 的新手,在检查输入是否已填写时遇到问题。

所以问题来了:我有一个标签位于输入上方 (position: absolute)。在焦点和输入被填充时,标签转到输入的顶部。但是如果我删除输入上的文本,标签会留在输入的顶部。

所以我需要检查输入是否已填写,但我的解决方案不起作用。 (标签应该回到输入的中心)

任何帮助将不胜感激:)

document.querySelector('.form__input').addEventListener('blur', onInputBlur);

function onInputBlur(ev) {

  if (ev.target && ev.target.value) {
    console.log('is-full');
    ev.target.classList.add('input--filled');
  }

}
/* FORM WRAPPER */

.form__input--wrapper {
  width: 250px;
  position: relative;
  height: 2rem;
  margin: 3rem 0;
}


/* FORM INPUT */

.form__input {
  position: relative;
  background-color: transparent;
  width: 100%;
  margin: 0;
  height: 100%;
  padding: 0;
  border: 0;
  border-bottom: 1px solid deeppink;
  font-size: 15px;
}

.form__input:focus {
  outline: none;
}

.form__input:focus+.form__label,
.input--filled+.form__label {
  top: -100%;
  align-items: flex-end;
  text-transform: uppercase;
  font-size: 11px;
}

.form__input:focus+.form__label::after,
.input--filled+.form__label::after {
  width: 100%;
  bottom: calc(-100% - 0.2rem);
}


/* FORM LABEL */

.form__label {
  position: absolute;
  top: 0;
  left: 0;
  color: #404d5b;
  display: flex;
  align-items: center;
  transition: 0.3s;
  z-index: -1;
  line-height: 1;
  font-family: Raleway;
  font-weight: 500;
  font-size: 15px;
  height: 100%;
  width: 100%;
}

.form__label::after {
  width: 0;
  height: 0.2rem;
  background-color: deeppink;
  position: absolute;
  transition: 0.3s;
  content: '';
  left: 0;
  bottom: -0.2rem;
}
<form class="form-wrapper">

  <div class="form__input--wrapper">
    <input type="text" class="form__input">
    <label for="name" class="form__label" id="a">My awesome label</label>
  </div>

</form>

【问题讨论】:

  • 目前做错了什么?

标签: javascript html css flexbox


【解决方案1】:

如果您的字段为空,则应添加ev.target.classList.remove('input--filled');

document.querySelector('.form__input').addEventListener( 'blur', onInputBlur );

function onInputBlur( ev ) {

	if(ev.target && ev.target.value) {
  	console.log('is-full');
		ev.target.classList.add('input--filled');
	} else {
         ev.target.classList.remove('input--filled');
    }

}
/* FORM WRAPPER */

.form__input--wrapper{
  width: 250px;
  position: relative;
  height: 2rem;
  margin: 3rem 0;
}

/* FORM INPUT */

.form__input{
  position: relative;
  background-color: transparent;
  width: 100%;
  margin: 0;
  height: 100%;
  padding: 0;
  border: 0;
  border-bottom: 1px solid deeppink;
  font-size: 15px;
}

.form__input:focus{
  outline: none;
}

.form__input:focus + .form__label,
.input--filled + .form__label{
  top: -100%;
  align-items: flex-end;
  text-transform: uppercase;
  font-size: 11px;
}

.form__input:focus + .form__label::after, .input--filled + .form__label::after{
  width: 100%;
  bottom: calc(-100% - 0.2rem);
}

/* FORM LABEL */

.form__label{
  position: absolute;
  top: 0;
  left: 0;
  color: #404d5b;
  display: flex;
  align-items: center;
  transition: 0.3s;
  z-index: -1;
  line-height: 1;
  font-family: Raleway;
  font-weight: 500;
  font-size: 15px;
  height: 100%;
  width: 100%;
}

.form__label::after{
  width: 0;
  height: 0.2rem;
  background-color: deeppink;
  position: absolute;
  transition: 0.3s;
  content: '';
  left: 0;
  bottom: -0.2rem;
}
<form class="form-wrapper">

  <div class="form__input--wrapper">
    <input type="text" class="form__input">
    <label for="name" class="form__label" id="a">My awesome label</label>
  </div>
  
</form>

【讨论】:

  • 虽然您的解决方案有效,但值得添加一些细节,说明您为解决 OP 的问题所做的工作
【解决方案2】:

只需再次删除事件侦听器中的类。

document.querySelector('.form__input').addEventListener( 'blur', onInputBlur );

function onInputBlur( ev ) {

	if(ev.target && ev.target.value) {
  	console.log('is-full');
		ev.target.classList.add('input--filled');
	} else {
        console.log('is-empty');
        ev.target.classList.remove('input--filled');
    }

}
/* FORM WRAPPER */

.form__input--wrapper{
  width: 250px;
  position: relative;
  height: 2rem;
  margin: 3rem 0;
}

/* FORM INPUT */

.form__input{
  position: relative;
  background-color: transparent;
  width: 100%;
  margin: 0;
  height: 100%;
  padding: 0;
  border: 0;
  border-bottom: 1px solid deeppink;
  font-size: 15px;
}

.form__input:focus{
  outline: none;
}

.form__input:focus + .form__label,
.input--filled + .form__label{
  top: -100%;
  align-items: flex-end;
  text-transform: uppercase;
  font-size: 11px;
}

.form__input:focus + .form__label::after, .input--filled + .form__label::after{
  width: 100%;
  bottom: calc(-100% - 0.2rem);
}

/* FORM LABEL */

.form__label{
  position: absolute;
  top: 0;
  left: 0;
  color: #404d5b;
  display: flex;
  align-items: center;
  transition: 0.3s;
  z-index: -1;
  line-height: 1;
  font-family: Raleway;
  font-weight: 500;
  font-size: 15px;
  height: 100%;
  width: 100%;
}

.form__label::after{
  width: 0;
  height: 0.2rem;
  background-color: deeppink;
  position: absolute;
  transition: 0.3s;
  content: '';
  left: 0;
  bottom: -0.2rem;
}
<form class="form-wrapper">

  <div class="form__input--wrapper">
    <input type="text" class="form__input">
    <label for="name" class="form__label" id="a">My awesome label</label>
  </div>
  
</form>

【讨论】:

    【解决方案3】:

    如果输入为 empty,您必须 删除该类 - 请参阅下面的演示:

    document.querySelector('.form__input').addEventListener('blur', onInputBlur);
    
    function onInputBlur(ev) {
    
      if (ev.target && ev.target.value.trim().length > 0) {
        ev.target.classList.add('input--filled');
      } else {
        ev.target.classList.remove('input--filled');
      }
    
    }
    /* FORM WRAPPER */
    
    .form__input--wrapper {
      width: 250px;
      position: relative;
      height: 2rem;
      margin: 3rem 0;
    }
    
    
    /* FORM INPUT */
    
    .form__input {
      position: relative;
      background-color: transparent;
      width: 100%;
      margin: 0;
      height: 100%;
      padding: 0;
      border: 0;
      border-bottom: 1px solid deeppink;
      font-size: 15px;
    }
    
    .form__input:focus {
      outline: none;
    }
    
    .form__input:focus+.form__label,
    .input--filled+.form__label {
      top: -100%;
      align-items: flex-end;
      text-transform: uppercase;
      font-size: 11px;
    }
    
    .form__input:focus+.form__label::after,
    .input--filled+.form__label::after {
      width: 100%;
      bottom: calc(-100% - 0.2rem);
    }
    
    
    /* FORM LABEL */
    
    .form__label {
      position: absolute;
      top: 0;
      left: 0;
      color: #404d5b;
      display: flex;
      align-items: center;
      transition: 0.3s;
      z-index: -1;
      line-height: 1;
      font-family: Raleway;
      font-weight: 500;
      font-size: 15px;
      height: 100%;
      width: 100%;
    }
    
    .form__label::after {
      width: 0;
      height: 0.2rem;
      background-color: deeppink;
      position: absolute;
      transition: 0.3s;
      content: '';
      left: 0;
      bottom: -0.2rem;
    }
    <form class="form-wrapper">
    
      <div class="form__input--wrapper">
        <input type="text" class="form__input">
        <label for="name" class="form__label" id="a">My awesome label</label>
      </div>
    
    </form>

    【讨论】:

      【解决方案4】:

      您需要else 才能删除class

      document.querySelector('.form__input').addEventListener( 'blur', onInputBlur );
      
      function onInputBlur( ev ) {
      
      	if(ev.target && ev.target.value) {
        	console.log('is-full');
      		ev.target.classList.add('input--filled');
      	} else {
          console.log('is-empty');
          ev.target.classList.remove('input--filled');
        }
      
      }
      /* FORM WRAPPER */
      
      .form__input--wrapper{
        width: 250px;
        position: relative;
        height: 2rem;
        margin: 3rem 0;
      }
      
      /* FORM INPUT */
      
      .form__input{
        position: relative;
        background-color: transparent;
        width: 100%;
        margin: 0;
        height: 100%;
        padding: 0;
        border: 0;
        border-bottom: 1px solid deeppink;
        font-size: 15px;
      }
      
      .form__input:focus{
        outline: none;
      }
      
      .form__input:focus + .form__label,
      .input--filled + .form__label{
        top: -100%;
        align-items: flex-end;
        text-transform: uppercase;
        font-size: 11px;
      }
      
      .form__input:focus + .form__label::after, .input--filled + .form__label::after{
        width: 100%;
        bottom: calc(-100% - 0.2rem);
      }
      
      /* FORM LABEL */
      
      .form__label{
        position: absolute;
        top: 0;
        left: 0;
        color: #404d5b;
        display: flex;
        align-items: center;
        transition: 0.3s;
        z-index: -1;
        line-height: 1;
        font-family: Raleway;
        font-weight: 500;
        font-size: 15px;
        height: 100%;
        width: 100%;
      }
      
      .form__label::after{
        width: 0;
        height: 0.2rem;
        background-color: deeppink;
        position: absolute;
        transition: 0.3s;
        content: '';
        left: 0;
        bottom: -0.2rem;
      }
      <form class="form-wrapper">
      
        <div class="form__input--wrapper">
          <input type="text" class="form__input">
          <label for="name" class="form__label" id="a">My awesome label</label>
        </div>
        
      </form>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-30
        • 1970-01-01
        • 1970-01-01
        • 2014-03-01
        • 2020-04-04
        • 2014-07-16
        • 2015-11-19
        相关资源
        最近更新 更多