我知道如何更改占位符,但如果需要,如何将其改回来?
我已经尝试过了,但它没有改变它..
$('[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/