Get default value of an input using jQuery

2019-02-11 17:55发布

$(".box_yazi2").each(function () {
    var default_value = this.value;
    $(this).css('color', '#555'); // this could be in the style sheet instead
    $(this).focus(function () {
        if (this.value == default_value) {
            this.value = '';
            $(this).css('color', '#000');
        }
    });
    $(this).blur(function () {
        if (this.value == '') {
            $(this).css('color', '#555');
            this.value = default_value;
        }
    });
});

This function of default value of input doesnt work in FF, but perfectly works in IE and ofcourse the input itself looks like this:

<input type="text" class="box_yazi2" id="konu" name="konu" value="Boş" />

6条回答
手持菜刀,她持情操
2楼-- · 2019-02-11 18:13

You should use prop instead of so many functions to be honest, use 'delegate' instead of 'on' for late static binding.

$('.box_yazi2').each(function() {

$(this).on('focus', function(){

   if($(this).val() == $(this).prop('defaultValue')){

      $(this).val('');
      $(this).css('color', '#000');
    }

});

$(this).on('blur', function(){

   if($(this).val() == ''){

      $(this).val($(this).prop('defaultValue'));
      $(this).css('color', '#000');
   }

});

});
查看更多
混吃等死
3楼-- · 2019-02-11 18:14

I'm using the next code:

    //clear the focused inputs
$('input[type="text"]').focus( function(){
    if( $(this).attr('value') == $(this).attr('defaultValue') ){
        $(this).attr('value', '');
    };
} );
$('input[type="text"]').blur( function(){
    if( $(this).attr('value') == '' ){
        $(this).attr('value', $(this).attr('defaultValue') );
    };
} );
查看更多
可以哭但决不认输i
4楼-- · 2019-02-11 18:22
$('input[type="text"]').focus( function(){
            elementValue = $(this).val();
            $(this).val("");
        });
        $('input[type="text"]').blur( function(){
            if($(this).val() != elementValue && $(this).val() != ""){

            }else{
                $(this).val(elementValue);
            }

        });
查看更多
成全新的幸福
5楼-- · 2019-02-11 18:26

The solution is quite easy; you have an extra }); in your code (thanks @ Box9).

I would encourage you to reuse the variable and not create dozens of jQuery objects.

I've changed your example to background-color but it will work.

$('.box_yazi2').each(function(index, element) {
    var $element = $(element);
    var defaultValue = $element.val();
    $element.css('background-color', '#555555');
    $element.focus(function() {
        var actualValue = $element.val();
        if (actualValue == defaultValue) {
            $element.val('');
            $element.css('background-color', '#3399FF');
        }
    });
    $element.blur(function() {
        var actualValue = $element.val();
        if (!actualValue) {
            $element.val(defaultValue);
            $element.css('background-color', '#555555');
        }
    });
});

demo

查看更多
Emotional °昔
6楼-- · 2019-02-11 18:33

Use this.defaultValue

Sorry for the link to w3notcools, http://www.w3schools.com/jsref/prop_text_defaultvalue.asp

查看更多
小情绪 Triste *
7楼-- · 2019-02-11 18:35

Just use the defaultValue property:

var default_value = $(this).prop("defaultValue");

Or:

var default_value = this.defaultValue;
查看更多
登录 后发表回答