`

Create a Clearable TextBox with jQuery

阅读更多

文章源自:http://viralpatel.net/blogs/clearable-textbox-jquery/

Create a Clearable TextBox with jQuery

Recently I came across a wonderful article by David Walsh on Clearable Textboxes with Dojo Toolkit [link]. This is a simple implementation where a clear button (x) added in a textbox which lets user to clear the content of the textbox. I have seen this simple feature in many websites and feel its very useful to have. It increase the usability of form.

I thought to implement the same feature using jQuery. Here is a simple plugin that I wrote to add clearable feature to any textbox in your HTML form. All you need is to call clearable() method on the textbox. For example.

...
<script type="text/javascript" src="jquery.clearable.js"></script>
<link rel="stylesheet" href="jquery.clearable.css" 
       type="text/css" media="screen" />
...
<input type="text"  class="foo"></input>
 
<script>
$(document).ready(function() {
	$('.foo').clearable();
});
</scrip>

 

In above example we included jquery.clearable plugin javascript and stylesheet files. Then called .clearable() method on the textboxes we want to add clearable feature. That’s it :-)

Let us go through the code and see what exactly goes behind the scene in jquery clearable plugin.

The CSS

We use following stylesheet classes internally to display the

.divclearable {
	border: 1px solid #888;
	display: -moz-inline-stack;
	display: inline-block;
	zoom:1;
	*display:inline;
	padding-right:5px;
	vertical-align:middle;
}
 
a.clearlink {
	background: url("close-button.png") no-repeat scroll 0 0 transparent;
	background-position: center center;
	cursor: pointer;
	display: -moz-inline-stack;
	display: inline-block;
	zoom:1;
	*display:inline;
	height: 12px;
	width: 12px;
	z-index: 2000;
	border: 0px solid;
}

 There are two css classes we uses in clearable plugin. First one is for a wrapper DIV which wraps around the textbox and clear button (x). And the style class is for clear button (x).

The JavaScript

The important bit of the plugin is javascript code that creates clearable effect. For everytextbox, we wrap it around a DIV container and also add one clear button (x) in this container DIV.

Here is the jQuery code:

$(this).css({'border-width': '0px', 'outline': 'none'})
		.wrap('<div id="sq" class="divclearable"></div>')
		.parent()
		.attr('class', $(this).attr('class') + ' divclearable')
		.append('<a class="clearlink" href="javascript:"></a>');
 
	$('.clearlink')
		.attr('title', 'Click to clear this textbox')
		.click(function() {
 
			$(this).prev().val('').focus();
 
	});

 

In above javascript code, we do:

  1. Remove border and outline of the textbox where we want to add clearable feature
  2. Create a DIV wrapper and wrap the textbox with that DIV
  3. Add the textbox style class to DIV, so the border / background etc that are specified on textbox are added to DIV
  4. Create a link for clear text(x) and append in the DIV

Also for each clear text link (x) we add an onclick handler where we clear the corresponding textbox and set focus in it.

 

 

 

  • 大小: 956 Bytes
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Drag and Drop Inserting Text to Input Textbox with jQuery.zip

    在这个"Drag and Drop Inserting Text to Input Textbox with jQuery"的项目中,我们关注的是如何利用jQuery库来实现这一功能,特别是在输入文本框中插入拖放文本的场景。jQuery是一个流行的JavaScript库,它简化了...

    TextBox Jquery日期控件

    本教程将详细讲解如何使用TextBox结合Jquery的WdatePicker插件实现一个方便快捷的日期选择器。 首先,`TextBox`是ASP.NET中的一个控件,用于创建用户可以输入文本的HTML元素。在JavaScript中,我们可以对这个控件...

    Add Textbox Dynamically using jQuery.zip

    "Add Textbox Dynamically using jQuery.zip"这个压缩包中的示例,主要展示了如何利用jQuery在用户交互时动态创建一个新的文本输入框。 首先,jQuery库是一个强大的JavaScript库,它简化了DOM操作、事件处理、动画...

    TextBox component

    TextBox组件在软件开发中扮演着至关重要的角色,尤其是在使用C#进行Windows Forms或WPF应用程序开发时。这个组件提供了一个用户界面元素,允许用户输入文本数据。在标题中提到的"Powerful",意味着我们将讨论TextBox...

    EasyUI textbox事件,EasyUI textbox input events

    在使用UasyUI的时候,在一般的input输入框上添加class="easyui-textbox"属性,则在该输入框上添加的事件将不起作用,但是如果去掉class="easyui-textbox"变成一般的input则起作用。 控件的生成原理,它是把你原有的...

    easyui-textbox和easyui-combobox的onchange事件响应实例

    在前端开发中,EasyUI 是一个基于 jQuery 的 UI 框架,它提供了一系列美观、易用的组件,用于构建用户界面。在这个实例中,我们将深入探讨 EasyUI 中的两个重要组件:`easyui-textbox` 和 `easyui-combobox`,以及...

    C# 几个textbox 里的值相加 赋值到 textbox

    在C#编程中,开发Windows桌面应用程序时,我们经常需要处理用户输入的数据,例如通过TextBox控件获取用户的数字输入,并进行一些计算操作。本话题主要关注如何实现多个TextBox中的数值相加,然后将结果实时显示在另...

    C# 禁用textbox后 改变text的颜色 TextBox

    /// the contents of this method with the code editor. /// private void InitializeComponent() { // ... } #endregion } ``` 尽管`myTextBox.Designer.cs`文件没有包含关于`DisableForeColor`属性的...

    asp.net的漂亮textbox控件

    3. JavaScript/jQuery插件:除了CSS,还可以使用JavaScript库如jQuery来增强`TextBox`的功能。例如,使用jQuery Mask Plugin可以实现复杂的输入格式约束,如电话号码、日期等。 4. HTML5新特性:HTML5引入了一些新...

    listview内嵌textbox(c#)

    为了实现类似电子表格那样的编辑功能,我们可以将TextBox控件内嵌到ListView的每一项中,使用户能够直接在ListView上修改数据。这种技术在C#编程中尤为常见,尤其在需要对大量数据进行查看和编辑的应用中。 首先,...

    C# TextBox 控件透明

    在C#编程中,TextBox控件是用于用户输入文本的标准组件。然而,有时开发者可能希望实现TextBox控件的透明效果,以提供更具视觉吸引力或特殊设计的用户界面。本篇文章将详细探讨如何在C#中实现TextBox控件的透明化,...

    asp.net中TextBox获得焦点和失去焦点——客户端JavaScript事件

    例如,使用`PageMethods`或者`jQuery AJAX`,可以在TextBox失去焦点时异步验证用户输入,而不需刷新整个页面。 ```javascript &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;&lt;/asp:ScriptManager&gt; &lt;asp:...

    动态增加删除TextBox组件

    本话题主要关注动态增加和删除TextBox控件,这是一种增强用户交互和提供灵活输入方式的有效手段。下面我们将深入探讨这个主题。 首先,动态增加TextBox控件通常是通过编程实现的,而不是在设计时直接在界面上放置。...

    winform下TextBox自动提示功能

    以下是一个简单的示例代码片段,展示了如何使用TextBox的TextChanged事件和StartsWith方法实现基本的自动提示功能: ```csharp private List&lt;string&gt; suggestions = new List() { "Apple", "Banana", "Cherry" }; /...

    textbox垂直居中

    例如,在窗体加载或大小改变事件中,计算Panel的高度减去TextBox的高度,然后将TextBox的Top属性设置为(Panel.Height - TextBox.Height)/ 2。这种方式更加灵活,可以适应各种情况。 4. **使用FlowLayoutPanel或...

    TextBoxValidator 验证TextBox 扩展TextBox属性

    在.NET框架中,Windows Forms应用程序经常使用TextBox控件来接收用户输入的数据。然而,为了确保数据的正确性和安全性,开发者需要对用户输入进行验证。"TextBoxValidator"是针对这种需求的一种自定义解决方案,旨在...

    WPF对TextBox输入内容校验

    在Windows Presentation Foundation (WPF)框架中,TextBox控件是用户界面中常见的元素,用于接收用户的文本输入。然而,为了确保应用程序的数据质量和安全性,开发者通常需要对TextBox输入的内容进行校验。本文将...

    TextBox记录上次输入值

    在.NET开发环境中,文本框(TextBox)是常用的一种控件,用于接收用户的文本输入。然而,标准的TextBox控件并没有内置的功能来记住用户上次输入的内容。为了实现这一功能,开发者通常需要自定义控件或者使用扩展方法...

    基于easyui-textbox的颜色选择器及源码

    EasyUI是一个基于jQuery的轻量级前端框架,它提供了丰富的UI组件,使得开发者可以快速构建出美观、易用的Web应用界面。其中,TextBox作为基本的输入控件,经常用于用户输入文本。然而,有时候我们需要在TextBox中...

    C#TEXTBOX的扩展

    可以创建一个自定义的TextBox类,继承自TextBox,并添加一个公共属性如WatermarkText,然后在Paint事件中绘制这个水印文本,确保在TextBox无文本时显示。 在提供的代码文件中,`TextBoxEnter.cs`和`TextBoxEnter....

Global site tag (gtag.js) - Google Analytics