0

以下のスクリプトコードはゴーストテキストを作成します

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

ただし、送信時に、デフォルトのゴースト値を渡します。送信時に削除するにはどうすればよいですか?

4

4 に答える 4

2

プレースホルダーを使用する方が良いと思います:

<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('');
    });
});
}
于 2012-12-04T13:10:34.333 に答える
0

このようなことを試しましたか?

$("#submit").click(function(){
    $(".ghost-text").each(function(){
        $(this).val('');
    }
})
于 2012-12-04T13:07:42.953 に答える
0
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('');
    });
于 2012-12-04T13:07:49.720 に答える
0

'ghot text' 透かしを呼び出し、HTML5 を使用できる場合は、代わりにプレースホルダー属性を使用します。

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

「ゴーストテキスト」が透かしとは関係なく、送信したくない場合は、代わりにこれを使用できます。

$('form').submit(function(){
    $('.ghost-text',this).each(function(){
        $(this).removeAttr('name');
    });
});
于 2012-12-04T13:12:15.307 に答える