zoukankan      html  css  js  c++  java
  • 自定义控件之 TextBox

    //textbox type
    var boxType = {
    WaterMarkBox: 0,
    ValidateBox: 1,
    SearchBox: 2
    }
    var textBoxObj = function(vid){
    this.id = vid; //textbox's id
    this.validateString = ""; //validate string
    this.waterMarkString = "please fill the content"; //watermark string
    this.width = 300; //textbox width
    this.height = 22; //textbox height
    this.type = boxType.WaterMarkBox; //textbox type
    this.imgUrl = "graphics/search.png";
    }

    //set textbox width
    textBoxObj.prototype.setWidth = function (wid) { this.width = wid };

    //set textbox height
    textBoxObj.prototype.setHeight = function (hei) { this.height = hei };

    //set textbox type
    textBoxObj.prototype.setType = function (tp) { this.type = tp };

    //initial textbox
    textBoxObj.prototype.initBox = function () {
    var context = this;
    if (context.type == boxType.WaterMarkBox) {
    var $textbox = $("<input id='" + context.id + "_ipt' type='text' style='" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;color:#cacacd;'/>");
    $("#" + context.id).append($textbox);
    $textbox.val(context.waterMarkString);
    $textbox.on("focus", function () {
    if ($textbox.val() == context.waterMarkString) {
    $textbox.val("");
    }
    $textbox.attr("style", "" + context.width + "px; height:" + context.height + "px;border: 1px solid #C0FF00;");
    });
    $textbox.on("focusout", function () {
    if ($textbox.val() == "") {
    $textbox.val(context.waterMarkString);
    $textbox.attr("style", "" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;color:#cacacd;");
    }
    else
    $textbox.attr("style", "" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;");
    });
    }
    else if (context.type == boxType.ValidateBox) {
    var $textbox_validate = $("<input id='" + context.id + "_ipt' type='text' style='" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;'/>");
    $("#" + context.id).append($textbox_validate);
    $textbox_validate.on("change",function () {
    var string = $textbox_validate.val().trim();
    if (string == "") {
    $textbox_validate.attr("style", "" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;")
    }
    else if (string.match(context.validateString) == null) {
    $textbox_validate.attr("style", "" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;background-color: #FC6B6B;background-image: url(graphics/fault.png);background-repeat: no-repeat;background-position: right center;");
    }
    else {
    $textbox_validate.attr("style", "" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;background-image: url(graphics/pass.png);background-repeat: no-repeat;background-position: right center;");
    }
    });

    }
    else if (context.type == boxType.SearchBox) {
    $textbox_search = $("<input type='text' style=' " + context.width + "px; height:22px;border: 1px solid #95B8E7; background-image: url("+context.imgUrl+");background-repeat: no-repeat;background-position: right center;' />");
    $("#" + context.id).append($textbox_search);
    $textbox_search.on("keydown", function (key) {
    if (key.keyCode == "13") {
    context.onSearch($textbox_search.val());
    }
    });
    }
    }

    //press enter and search event
    textBoxObj.prototype.onSearch = function (content) {
    alert(content);
    }

    //set validate rule
    textBoxObj.prototype.setValidateString = function (rule) {
    this.validateString = rule;
    }

    //set watermark message
    textBoxObj.prototype.setWaterMarkString = function (watemark) {
    this.waterMarkString = watemark;
    }

    //for test
    //$(function () {
    // var o = new textBoxObj('cc2');
    // o.setType(boxType.ValidateBox);
    // o.initBox();
    // o.setValidateString("^[0-9]*$");
    //});

  • 相关阅读:
    C# Note23: 如何自定义类型使用foreach循环
    C# Note22: 《Effective C#》笔记
    C# Note21: 扩展方法(Extension Method)及其应用
    C# Note20: 制作延时改变显示的标题栏
    C# Note19: Windows安装包制作实践
    Python Note1: Pycharm的安装与使用
    java Html&JavaScript面试题:HTML 的 form 提交之前如何验证数值文本框的内容全部为数字? 否则的话提示用户并终止提交?
    java Html&JavaScript面试题:用table显示n条记录,每3行换一次颜色,即1,2,3用红色字体,4,5,6用绿色字体,7,8,9用红颜色字体。
    java Html&JavaScript面试题:判断第二个日期比第一个日期大
    java算法面试题:金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。
  • 原文地址:https://www.cnblogs.com/jin256/p/3216486.html
Copyright © 2011-2022 走看看