﻿
// 输入框提示效果
$(function () {
    var initInputTip = function () {
        var elements = $('input[tipText]');
        elements.each(function () {
            var form = $(this).parents("form")[0];
            var v = $(this).val();
            this.hasInput = v != "";
            //insert tip element and locate related elements
            $(this).parent().css("position", "relative");
            $(this).attr("autocomplete", "off").css("position", "relative").css("background", "transparent url('data:image/gif;base64,R0lGODlhAQADAJEAAObm5t3d3ff39wAAACH5BAAAAAAALAAAAAABAAMAAAICDFQAOw==') 0 0 repeat-x").css("z-index", "2");
            $(this).before("<label style='position:absolute;top:2px;display:none;left:4px;z-index:1'>" + $(this).attr("tipText") + "</label>");
            //fix position
            var prev = $(this).prev().addClass("jq_input_tip"); ;
            prev.css("top", ($(this).height() - prev.height()) / 2);
            //when post back,hide tip text
            if (!this.hasInput) prev.show();
            $(this).focus(function () {
                if (!this.hasInput) {
                    $(this).prev().hide(); //hide tip text
                }
            }).blur(function () {
                var v = $(this).val();
                if (v == "" || v == null)
                    this.hasInput = false;
                else
                    this.hasInput = true;
                if (!this.hasInput) {
                    $(this).prev().show(); // show tip text
                }
            }).keydown(function (e) {
                //Enter key, submit the form
                if (e.keyCode == 13) {
                    $(form).submit();
                } else {
                    this.hasInput = true;
                }
            });
        })
    }
    initInputTip();
})
