javascript - Jquery - 联系人字段交互

标签 javascript jquery html css

大家好,我正在尝试创建 contect fielded 交互。当我们点击任何字段时,标签文本应该上升。我试过但没有用,为了更清楚起见,我还附上了我的代码。你们能帮帮我吗?谢谢:)

$('.form-grid').keyup(function() {
  if ($(this).val()) {
    $(this).addClass('active');
  } else {
    $(this).removeClass('active');
  }
});
.form-grid {
  max-width: 200px;
  position: relative;
  margin-bottom: 25px;
}

.form-grid input,
.form-grid textarea {
  height: 50px;
  width: 100%;
  border: none;
  border-bottom: 1px solid #000000;
}

.form-grid label {
  position: absolute;
  top: 0;
  left: 0;
  margin: 0;
}

.form-grid.active label {
  top: -15px;
}
<div class="form-grid">
  <input type="text">
  <label> Name </label>
</div>

<div class="form-grid">
  <input type="email">
  <label> Email </label>
</div>

<div class="form-grid">
  <input type="tel">
  <label> Phone </label>
</div>

<div class="form-grid">
  <textarea></textarea>
  <label> Message </label>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

最佳答案

那是因为代码中的 $(this) 元素实际上引用了 div.form-grid 节点,它没有附加值。你想要的是选择嵌套的输入/文本区域元素,即 $(this).find('input, textarea').first().val():

$('.form-grid').keyup(function() {
  if ($(this).find('input, textarea').first().val()) {
    $(this).addClass('active');
  } else {
    $(this).removeClass('active');
  }
});

请参阅下面的概念验证:

$('.form-grid').keyup(function() {
  if ($(this).find('input, textarea').first().val()) {
    $(this).addClass('active');
  } else {
    $(this).removeClass('active');
  }
});
.form-grid {
  max-width: 200px;
  position: relative;
  margin-bottom: 25px;
}

.form-grid input,
.form-grid textarea {
  height: 50px;
  width: 100%;
  border: none;
  border-bottom: 1px solid #000000;
}

.form-grid label {
  position: absolute;
  top: 0;
  left: 0;
  margin: 0;
}

.form-grid.active label {
  top: -15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-grid">
  <input type="text">
  <label> Name </label>
</div>

<div class="form-grid">
  <input type="email">
  <label> Email </label>
</div>

<div class="form-grid">
  <input type="tel">
  <label> Phone </label>
</div>

<div class="form-grid">
  <textarea></textarea>
  <label> Message </label>
</div>


但是,我建议不要将 keyup 事件监听器绑定(bind)到包装 div.form-grid。您可以做的是将 input 事件监听器绑定(bind)到 div.form-grid 中的 input 元素,然后只需切换父元素上的类即可:

$('.form-grid input, .form-grid textarea').on('input', function() {
  var $formGrid = $(this).closest('.form-grid');
  if (this.value)
    $formGrid.addClass('active');
  else
    $formGrid.removeClass('active');
});
.form-grid {
  max-width: 200px;
  position: relative;
  margin-bottom: 25px;
}

.form-grid input,
.form-grid textarea {
  height: 50px;
  width: 100%;
  border: none;
  border-bottom: 1px solid #000000;
}

.form-grid label {
  position: absolute;
  top: 0;
  left: 0;
  margin: 0;
}

.form-grid.active label {
  top: -15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-grid">
  <input type="text">
  <label> Name </label>
</div>

<div class="form-grid">
  <input type="email">
  <label> Email </label>
</div>

<div class="form-grid">
  <input type="tel">
  <label> Phone </label>
</div>

<div class="form-grid">
  <textarea></textarea>
  <label> Message </label>
</div>

关于javascript - Jquery - 联系人字段交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54321638/

相关文章:

javascript - 动态绑定(bind)挖空图片源

javascript - 如何在不重新加载页面的情况下更改框架源

javascript - 返回 JavaScript 中的函数输入

javascript - 提交输入和 jQuery 验证的问题

javascript - 如何利用原型(prototype)继承而不需要按特定顺序包含文件?

javascript - 对象 #<HTMLLIElement> 没有方法 'bind'

jquery - 将鼠标悬停在元素上时防止下拉菜单折叠

javascript - 从另一个js文件的函数调用js文件中的javascript函数

html - 如何为文本添加半透明突出显示?

javascript - 加载 JSON 作为 HTML 中的资源以避免引导 ajax