How to keep some input text removed by jquery when going back with browser?

前端 未结 4 1522
滥情空心
滥情空心 2021-01-20 10:44

I have some bug with the following page: http://jsbin.com/agedu/ Source code with some comments: http://jsbin.com/agedu/edit

The problem is that when typing somethin

4条回答
  •  礼貌的吻别
    2021-01-20 11:13

    The inputdynvalue() function explicitly forces the textbox text when called, even if the textbox is non-empty.

    Fix it like so:

     (function($) {
        $.fn.inputdynvalue = function(options) {
            var opts = $.extend({},
            $.fn.inputdynvalue.defaults, options);
            return this.each(function() {
                var hvalue = opts.htext;
                switch (opts.htext) {
                case 'title':
                    hvalue = $(this).attr('title');
                    break;
                case 'value':
                    hvalue = $(this).attr('value');
                    break;
                }
                if (this.value === '') {
                  $(this).attr('value', hvalue).addClass(opts.hclass)
                }
                $(this).unbind('focus blur').bind('focus',
                function() {
                    if (this.value === this.title) {
                        this.value = '';
                        $(this).removeClass(opts.hclass);
                    }
                }).bind('blur',
                function() {
                    if (this.value === '') {
                        this.value = this.title;
                        $(this).addClass(opts.hclass);
                    }
                });
            });
        };
        $.fn.inputdynvalue.defaults = {
            htext: 'title',
            hclass: 'hint'
        };
    })(jQuery);
    

    The key is the lines:

            if (this.value === '') {
              $(this).attr('value', hvalue).addClass(opts.hclass)
            }
    

    which replace the un-conditional overwrite from before.

    The fixed version can be seen at http://jsbin.com/ikipi.

提交回复
热议问题