【问题标题】:Jquery Datepicker placeholder not showing after blurJquery Datepicker占位符在模糊后不显示
【发布时间】:2015-07-16 11:13:23
【问题描述】:

我有简单的 2 文本字段。一个是普通文本,另一个我用于日期选择器。我想清除占位符 onfocus 并再次显示 onblur 这是我正在使用的以下代码:

$('input,textarea').focus(function(){
    $(this).data('placeholder',$(this).attr('placeholder'))
    console.log($(this).data('placeholder'));
    $(this).attr('placeholder','');
});
$('input,textarea').blur(function(){
    console.log("adding "+$(this).data('placeholder'));
    $(this).attr('placeholder',$(this).data('placeholder'));
});

对于普通文本,它可以正常工作,但对于 datepicker 文本字段占位符在模糊后不会出现。是否有任何解决方案使 datepicker 文本字段可以表现得像简单的文本字段

试试DEMO

【问题讨论】:

  • 第 10 行:$(this).data('placeholder', $(this).attr('placeholder'));缺少分号
  • @PhilAndelhofs 你确定是因为;吗?我不这么认为:-(
  • 刚刚对你的东西发表了评论,现在正在寻找选项.. 等一下
  • 我知道,但谢谢 :D 仍在寻找一个简单的解决方案,我认为成立

标签: jquery datepicker


【解决方案1】:

由于日期选择器而触发了两个焦点事件。第二次触发它会删除属性占位符。因此,只需在存储到 .data() 之前检查该属性。这样的事情应该这样做

$('input,textarea').focus(function () {
    if ($(this).prop('placeholder')) {
        $(this).data('placeholder', $(this).prop('placeholder'))
        $(this).prop('placeholder', '');
    }
});
$('input,textarea').blur(function () {
    if (!$(this).prop('placeholder')) {
        $(this).prop('placeholder', $(this).data('placeholder'));
    }
});

这是一个演示http://jsfiddle.net/dhirajbodicherla/ndnxznn3/25/

【讨论】:

  • 嗯,这就是我正在寻找的解决方案。
【解决方案2】:

您可以为日期选择器设置一个例外,这样占位符就不会被清除:

$('input:not(#thedate),textarea:not(#thedate)').focus(function(){

【讨论】:

  • 完全错误,我希望清除日期选择器占位符,并且应该像普通文本一样返回。
猜你喜欢
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多