【问题标题】:Display div after placeholder is removed during form input在表单输入期间删除占位符后显示 div
【发布时间】:2018-06-29 14:54:15
【问题描述】:

我想在客户输入表单时显示一个 div。我最初虽然 z-index 解决方案可以解决这个问题,但显示/显示选项似乎更好。

但是,它似乎不起作用。这可以缩短或以更好的方式完成吗?

    $(document).ready(function(){
    if ($("input#poundprice").text().length > 0) {
        $('.pence-sign').show();
        //$('.pence-sign').addClass('display:block;');
    }                                           
    });

这里是JSFiddle

【问题讨论】:

  • 当用户开始在框中输入或用户提交表单后
  • 这会检查输入是否在页面加载时填充,但它不会再次检查。您希望在什么时候显示 <div>
  • 您需要在您的问题中发布minimal reproducible example ,而不是在 jsFiddle 上。其次,当 DOM 准备好时运行 jQuery,但只要输入字段发生更改,就应该运行它

标签: javascript jquery html css forms


【解决方案1】:

您需要检查输入框的实际输入时间。您当前正在执行的操作仅在页面加载时运行一次。

$(document).ready(function(){
  $("#poundprice").on("change keydown paste",function(e) {
  	$('.pence-sign').show();  
  });
  
  $("button").click(function(e){
    e.preventDefault();
  });
});
.pence-sign {
  position: absolute;
  top: 23%;
  left: 30%;
  text-transform: lowercase;
  font-family: arial;
  display: none;
}

input, button {
    appearance: none;
    border: none;
    font-size: inherit;
    background: #eee;
    border-radius: 3px;
    padding: 1rem;
    width: 100%;
    max-width: 280px;
    margin-bottom:1rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form style="position:relative">
  <div class="poundinput" style="position:relative;">
    <p class="pence-sign">pence</p>
    <input id="poundprice" name="price" type="currency" placeholder="Enter p value" required="">
  </div>
  <button type="submit">Submit</button>
</form>

【讨论】:

  • 这不是正确的答案。提交后一切都消失了。
  • 你打败了我!虽然为了用户友好,我会在你的事件监听器中添加“粘贴”。还可以尝试添加 preventDefault 以便表单不会重置。
  • 用粘贴更新:)
  • 这几乎就在那里,但是在某些尺寸上,便士值与占位符位于同一位置。所以当他们点击输入框输入他们的值时,我需要占位符消失并显示便士值,试试这个input:focus::-webkit-input-placeholder { color:transparent; } input:focus:-moz-placeholder { color:transparent; }
  • 我不太确定你想要什么。您是否只希望占位符文本在关注输入时显示 pence?
猜你喜欢
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 2013-05-02
  • 2011-06-01
  • 2017-05-15
  • 2015-02-18
相关资源
最近更新 更多