【问题标题】:Change placeholder text with jquery also auto-update in IE使用 jquery 更改占位符文本也会在 IE 中自动更新
【发布时间】:2013-05-30 00:14:30
【问题描述】:

我使用占位符属性在输入字段中显示一些信息。为了在 IE 中支持这一点,我使用 jquery。 该代码在 IE 中工作。但是,根据用户在下拉列表中选择的内容,某些输入字段中占位符的值也需要更改,这就是问题所在。

调整占位符属性中的值不会自动更改输入字段的值。

我用来确定输入字段值的代码是:

if(!$.support.placeholder) { 
    var active = document.activeElement;
    $('input[type=text], textarea').focus(function () {
        if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
            $(this).val('').removeClass('hasPlaceholder');
        }
    }).blur(function () {
        if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
            $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
        }
    });

当我想更改属性文本时,我在一个名为 SetPlaceholderText 的函数中执行:

$("#affected-input-field").attr("placeholder", "New placeholder text");

但这并不能解决我的问题。

基本上我想做的是:

<input id="affected-input-field" value="Old placeholder text" placeholder="Old placeholder text" />

伪代码:

if($('#dropdown').val() == '1') {
    $(#affected-input-field).SetPLaceholderText("New Placeholder text...");
}

从代码运行的那一刻起,输入字段中可见的文本就是新值:

<input id="affected-input-field" value="New placeholder text" placeholder="New placeholder text" />

但仅当输入字段为空且尚未填写时。

我想知道,https://github.com/mathiasbynens/jquery-placeholder 的代码对我有帮助吗?因为据我所见,该代码包含一个更改占位符值的函数。

【问题讨论】:

    标签: jquery placeholder


    【解决方案1】:

    问题解决了,愚蠢的错误,在 IE 开发者工具中我没有看控制台。在那里我看到一个变量没有正确定义... epic facepalm.

    更准确地说: 我为 jQuery 更改占位符文本的扩展函数有一行:

    $(this).addClass(placeHolderStylingClass);
    

    但是变量 placeHolderStylingClass 没有在函数中设置,这是我的 setPlaceholder 函数中的一个愚蠢的复制/粘贴错误。

    【讨论】:

      猜你喜欢
      • 2012-03-03
      • 2013-02-04
      • 2020-11-28
      • 1970-01-01
      • 2018-09-19
      • 2014-08-19
      • 1970-01-01
      • 2014-11-22
      相关资源
      最近更新 更多