Jquery - 恢复更改的占位符?
Jquery - Restore Changed Placeholder?
我知道如何更改占位符,但如果需要如何将其改回原位?
我试过了,但没有改回来..
$('[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) ;
}
});
我知道如何更改占位符,但如果需要如何将其改回原位?
我试过了,但没有改回来..
$('[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) ;
}
});