/ / Manera fácil de eliminar el valor predeterminado del texto fantasma al enviar - javascript, jquery

Una forma sencilla de eliminar el valor predeterminado del texto fantasma en el envío: javascript, jquery

El siguiente código de script crea texto fantasma

$(".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");
}
});
});

Pero al enviar pasa ese valor fantasma predeterminado. ¿Cómo borrarlo al enviar?

Respuestas

2 para la respuesta № 1

Creo que es mejor usar un marcador de posición:

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

O si quieres seguir con tu 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("");
});
});
}

0 para la respuesta № 2

¿Has intentado algo como ésto?

$("#submit").click(function(){
$(".ghost-text").each(function(){
$(this).val("");
}
})

0 para la respuesta № 3
var ghostTexts = $(".ghost-text");

ghostTexts.each(function(){
var $this = $(this),
d = $(this).val();

$this.on({
"focus": function(){
if ($this.val() == d){
$this.val("").removeClass("ghost-text");
}
},
"blur": function() {
if ($this.val() == ""){
$this.val(d).addClass("ghost-text");
}
}
});
});

$("yourForm").on("submit", function() {
ghostTexts.val("");
});

0 para la respuesta № 4

Si llama a la marca de agua "ghot text" y puede usar HTML5, use el atributo de marcador de posición en su lugar:

<input type="text" placeholder="My ghost text" />

Si el "texto fantasma" no tiene nada que ver con la marca de agua y no desea enviarlo, puede usar esto en su lugar:

$("form").submit(function(){
$(".ghost-text",this).each(function(){
$(this).removeAttr("name");
});
});