---
title: "<转>Extjs中的提示信息用法"
author: "zpball"
date: "2012-04-18T09:43:10Z"
tags: ["EXT"]
categories: ["Web前端"]
slug: "extjs"
draft: false
lastmod: "2012-04-18T09:46:09Z"
id: 1489111
url: "https://zpball.iteye.com/blog/1489111"
---

ExtJS.form中msgTarget  

Ext表单提示方式：msgTarget:有4中方式：qtip,title,under,side

```js

Ext.onReady(function(){
 Ext.BLANK_IMAGE_URL="resources/images/default/s.gif";
 Ext.QuickTips.init();// 初始化显示提示信息。没有它提示信息出不来。
 Ext.form.Field.prototype.msgTarget = "side";


 var form = new Ext.form.FormPanel({
  title:"提示信息(side)",
  height:200,
  width:300,
  frame:true,
  labelSeparator:":",
  labelWidth:60,
  labelAlign:"right",
  items:[
   new ext.form.textfield({
    fieldlabel : "姓名",
    allowblank:false,
    blanktext:"请输入名字",
    msgtarget:"qtip"  //修改这里的值msgtarget:"title"  msgtarget:"under"  msgtarget:"side"
   }),
   new ext.form.numberfield({
    fieldlabel:"年龄",
    allowblank:false,
    blanktext:"请写年龄",
    msgtarget:"qtip"
   })
  ]
 });
 new Ext.Viewport({
  title:"",
  items:[form]
 });
});
```

如图：

![](http://dl.iteye.com/upload/attachment/0066/7603/1e03b478-93ed-3128-878d-9887d9ae18c5.jpg)


![](http://dl.iteye.com/upload/attachment/0066/7605/33aab4bc-8fb1-3dc0-a7dc-e6f0f5abdffb.jpg)

![](http://dl.iteye.com/upload/attachment/0066/7607/b06a8e45-830d-3430-a9f5-255d44da1c41.jpg)

![](http://dl.iteye.com/upload/attachment/0066/7609/d92a5452-e417-31df-a8c3-a572a6b9f3dd.jpg)

使用under时要注意表单的高度，高度不够的话就会出现以下情况：
图:

![](http://dl.iteye.com/upload/attachment/0066/7611/221be358-4a39-3867-b970-31d19afa9953.jpg)
使用side是要注意表单的宽度，宽度不够就会出现以下情况：
图:

![](http://dl.iteye.com/upload/attachment/0066/7613/7ebb3371-8cd2-3bf1-865b-bc5587f30bbe.jpg)
在每个字段上加提示方式很烦琐，
只要在Ext.QuickTips.init();下加一行Ext.form.Field.prototype.msgTarget = "under";//title,qtip,side
就可以实现统一的提示方式了。

```js

***********************************************************
※Ext.form.TextField※
Ext.onReady(function(){
 Ext.QuickTips.init();
 Ext.form.Field.prototype.msgTarget="side";
 var form = new Ext.form.FormPanel({
  title:"Ext.form.FormPanel例子",
  labelSeparator:":",
  labelWidth:60,
  bodyStyle:"padding:5 5 5 5",
  frame:true,
  height:120,
  width:250,
  items:[
   new ext.form.textfield({
    fieldlabel:"用户名",
    id:"username",
    selectonfocus:true,  //得到焦点时自动选择文本
    allowblank:false,
    regex:/^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([a-za-z]){2,4}$/,
    regexText:"用户名格式错误"
   }),
   new Ext.form.TextField({
    fieldLabel:"密码",
    inputType:"password",
    allowBlank:false
   })
  ]
 });
 new Ext.Viewport({
  title:"",
  items:[form]
 });
});
```

这里验证用户名必须是email格式的。先验证是否为空，然后在验证格式。


![](http://dl.iteye.com/upload/attachment/0066/7615/6edbafeb-aec4-311a-8ef0-ae0f27f078f5.jpg)


```js

***********************************************************
※Ext.form.TextArea※
Ext.onReady(function(){
 Ext.QuickTips.init();
 var form = new Ext.form.FormPanel({
  title:"Ext.form.TextArea例子",
  labelSeparator:":",
  labelWidth:60,
  bodyStyle:"padding:5 5 5 5",
  frame:true,
  height:150,
  width:250,
  items:[
   new ext.form.textarea({
    id:"memo",
    width:150,
    fieldlabel:"备注"
   })
  ],
  buttons:[{text:"确定",handler:showvalue}]
 });
 function showValue(){
  var memo = form.findById("memo"); //获得输入控件
  alert(memo.getValue());           //取得空间值
 };
 new Ext.Viewport({
  title:"",
  items:[form]
 });
});
```




![](http://dl.iteye.com/upload/attachment/0066/7617/228b5958-e82e-3f16-aa28-5131ca34efcf.jpg)

```js

***********************************************************
※Ext.form.NumberField※
Ext.onReady(function(){
 Ext.QuickTips.init();
 Ext.form.Field.prototype.msgTarget="side";
 var form = new Ext.form.FormPanel({
  title:"Ext.form.NumberField例子",
  labelSeparator:":",
  labelWidth:60,
  bodyStyle:"padding:5 5 5 5",
  frame:true,
  height:150,
  width:250,
  items:[
   new ext.form.numberfield({
    fieldlabel:"整数",
    allowdecimals:false,  //不允许输入小数
    nantext:"请输入有效数字", //无效数字提示
    allownegative:false       //不允许输入负数
   }),
   new ext.form.numberfield({
    fieldlabel:"小数",
    decimalprecision:2,  //精确到小数点后2位 
    allowdecimals:true,
    nantext:"请输入有效数字",
    allownegative:false
   }),
   new ext.form.numberfield({
    fieldlabel:"数字限制",
    basechars:"12345"  // 输入数字范围
   }),
   new ext.form.numberfield({
    fieldlabel:"数值限制",
    maxvalue:100,  //最大值
    minvalue:50    //最小值
   })
  ]
 });
 new Ext.Viewport({
  title:"",
  items:[form]
 });
});
```

decimalPrecision会四舍五入。当小数保留2位时，14.23545，焦点离开后会变成 14.24
![](http://dl.iteye.com/upload/attachment/0066/7619/d30fee43-1622-3fa4-bf6f-670189ab6bfe.jpg)


转载自：<http://blog.163.com/xiao_mege/blog/static/729427532011327104847120/>