gpt4 book ai didi

javascript - 删除提交时的幻影文本默认值的简单方法

转载 作者:行者123 更新时间:2023-12-02 19:11:04 28 4
gpt4 key购买 nike

下面的脚本代码制作幽灵文本

$('.ghost-text').each(function(){
var d = $(this).val();
$(this).focus(function(){
if ($(this).val() == d){
$(this).val('').removeClass('ghost-text');
}
});
$(this).blur(function(){
if ($(this).val() == ''){
$(this).val(d).addClass('ghost-text');
}
});
});

但是在提交时它会传递默认的幽灵值。如何在提交时删除它?

最佳答案

我认为使用占位符更好:

<input type="text" id="myTxt" placeholder="enter something..."/>

或者如果你想坚持使用你的js:

 if($.browser.msie){
$('.ghost-text').each(function(){
var d = $(this).attr('placeholder');
$(this).focus(function(){
if ($(this).val() == d){
$(this).val('').removeClass('ghost-text');
}
});
$(this).blur(function(){
if ($(this).val() == ''){
$(this).val(d).addClass('ghost-text');
}
});
});

$('form').submit(function(){
$('.ghost-text').each(function(){
if ($(this).val() == $(this).attr('placeholder'))
$(this).val('');
});
});
}

关于javascript - 删除提交时的幻影文本默认值的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13703557/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com