ExtJS 中,下列两个方法有差别
formPanel.getForm().getValues()
formPanel.getForm().getFieldValues()
1.对于下拉控件 ComboBox
前者可能拿不到下拉框的value内容,而只是拿到它显示的内容
后者则拿到value的内容。
2.对于DateField
前者同样是拿到显示的内容,
后者则是完整的datetime, 如 Tue Jun 18 2013 00:00:00 GMT+0800 (中国标准时间
Ext.urlEncode后,显示为这样的内容 2013-06-18T00:00:00
其实这些从他们的源代码就能看出来。内容如下:
Returns the fields in this form as an object with key/value pairs as they would be submitted using a standard form submit. * If multiple fields exist with the same name they are returned as an array.
*Note: The values are collected from all enabled HTML input elements within the form, not from * the Ext Field objects. This means that all returned values are Strings (or Arrays of Strings) and that the * value can potentially be the emptyText of a field.
* @param {Boolean} asString (optional) Pass true to return the values as a string. (defaults to false, returning an Object) * @return {String/Object} */ getValues : function(asString){
var fs = Ext.lib.Ajax.serializeForm(this.el.dom);
if(asString === true){
return fs;
}
return Ext.urlDecode(fs);
},
/** * Retrieves the fields in the form as a set of key/value pairs, using the {@link Ext.form.Field#getValue getValue()} method. * If multiple fields exist with the same name they are returned as an array. * @param {Boolean} dirtyOnly (optional) True to return only fields that are dirty. * @return {Object} The values in the form */
getFieldValues : function(dirtyOnly){
var o = {},
n,
key,
val;
this.items.each(function(f) {
if (!f.disabled && (dirtyOnly !== true || f.isDirty())) {
n = f.getName();
key = o[n];
val = f.getValue();
if(Ext.isDefined(key)){
if(Ext.isArray(key)){
o[n].push(val);
}else{
o[n] = [key, val];
}
}else{
o[n] = val;
}
}
});
return o;
},
--------------------Ext.lib.Ajax.serializeForm------------
encoder : encodeURIComponent,
serializeForm : function(){
var reSelect = /select-(one|multiple)/i,
reInput = /file|undefined|reset|button/i,
reChecks = /radio|checkbox/i,
reSubmit = /submit/i;
return function(form) {
var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
hasSubmit = false,
encoder = this.encoder,
element,
options,
name,
val,
data = '',
type, hasValue;
forEach(fElements, function(element) {
name = element.name;
type = element.type;
if (!element.disabled && name){
if(reSelect.test(type)){
forEach(element.options, function(opt) {
if (opt.selected) {
hasValue = opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttributeNode('value').specified;
data += String.format("{0}={1}&", encoder(name), encoder( hasValue ? opt.value : opt.text));
}
});
} else if(!reInput.test(type)) {
if(!(reChecks.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)){
data += encoder(name) + '=' + encoder(element.value) + '&';
hasSubmit = reSubmit.test(type);
}
}
}
});
return data.substr(0, data.length - 1);
};
}(),
浙公网安备 33010602011771号