Jquery - 恢复更改的占位符?

标签 jquery placeholder restore

我知道如何更改占位符,但如果需要,如何将其改回来?

我已经尝试过了,但它没有改变它..

$('[name^="u_"]').change(function(){
    if($(this).is(":checked")) {
        $(this).next("input, select").attr("disabled",true);
        $(this).next("input, select").attr("placeholder", "New Holder");

    } else {
        $(this).next("input, select").attr("disabled",false);
        $(this).next("input, select").attr('defaultValue') 
    }
});

我在多个输入上使用此代码。每个字段都有其默认占位符设置,使用:

<input type='checkbox' name='u_id'><input type="text" name="id" placeholder="ABC123">

谢谢。

最佳答案

您可以为旧占位符采用另一个属性,就像在我的代码中一样,当您希望将其用作旧占位符时,我采用 data-placeholder 来保存旧值。

HTML

<input type='checkbox' name='u_id'>
<input type="text" name="id" placeholder="ABC123" data-placeholder="ABC123">
                                                  ^^^^^^^^^^^

jQuery

$('[name^="u_"]').change(function(){
    if($(this).is(":checked")) {
        $(this).next("input, select").attr("disabled",true);
        $(this).next("input, select").attr("placeholder", "New Holder");

    } else {
        old_place_holder = $(this).next("input, select").attr("data-placeholder");
        $(this).next("input, select").attr("disabled",false);
        $(this).next("input, select").attr('placeholder',old_place_holder) ;
    }
});

<强> Demo

关于Jquery - 恢复更改的占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28896937/

相关文章:

postgresql - 如果不存在通过 postgres 备份和恢复工具创建数据库的问题

java - 是否可以在 android 中备份和恢复数据库文件?非根设备

Jquery 下拉菜单双下拉

jquery - 菜单导航不会使用 Jquery addClass() 更改样式

javascript - 是否可以在列表中的背景图像上触发 jQuery 单击事件?

html - IE 中的占位符相关问题

javascript - 如何在 jQuery 中恢复文本字段的默认值?

javascript - 无法与 <t :loop> (Tapestry ) 进行叠加

mysql - 为什么 Perl 的 DBI 占位符会因负数 MySQL 查询而中断?

html - 使用 simple-line-icons 符号作为占位符