dijit.form._FormMixin.getValues
dojo.require("dijit.form.Form");
defined in dijit/form/Form.js
generate JSON structure from form values
Usage
function () (view source)
// get widget values var obj = { }; dojo.forEach(this.getDescendants(), function(widget){ var name = widget.name; if(!name){ return; } // Single value widget (checkbox, radio, or plain <input> type widget var value = (widget.getValue && !widget._getValueDeprecated) ? widget.getValue() : widget.value; // Store widget's value(s) as a scalar, except for checkboxes which are automatically arrays if(typeof widget.checked == 'boolean'){ if(/Radio/.test(widget.declaredClass)){ // radio button if(value !== false){ dojo.setObject(name, value, obj); } }else{ // checkbox/toggle button var ary=dojo.getObject(name, false, obj); if(!ary){ ary=[]; dojo.setObject(name, ary, obj); } if(value !== false){ ary.push(value); } } }else{ // plain input dojo.setObject(name, value, obj); } }); /*** * code for plain input boxes (see also dojo.formToObject, can we use that instead of this code? * but it doesn't understand [] notation, presumably) var obj = { }; dojo.forEach(this.containerNode.elements, function(elm){ if (!elm.name) { return; // like "continue" } var namePath = elm.name.split("."); var myObj=obj; var name=namePath[namePath.length-1]; for(var j=1,len2=namePath.length;j<len2;++j){ var nameIndex = null; var p=namePath[j - 1]; var nameA=p.split("["); if (nameA.length > 1){ if(typeof(myObj[nameA[0]]) == "undefined"){ myObj[nameA[0]]=[ ]; } // if nameIndex=parseInt(nameA[1]); if(typeof(myObj[nameA[0]][nameIndex]) == "undefined"){ myObj[nameA[0]][nameIndex] = { }; } } else if(typeof(myObj[nameA[0]]) == "undefined"){ myObj[nameA[0]] = { } } // if if (nameA.length == 1){ myObj=myObj[nameA[0]]; } else{ myObj=myObj[nameA[0]][nameIndex]; } // if } // for if ((elm.type != "select-multiple" && elm.type != "checkbox" && elm.type != "radio") || (elm.type=="radio" && elm.checked)){ if(name == name.split("[")[0]){ myObj[name]=elm.value; } else{ // can not set value when there is no name } } else if (elm.type == "checkbox" && elm.checked){ if(typeof(myObj[name]) == 'undefined'){ myObj[name]=[ ]; } myObj[name].push(elm.value); } else if (elm.type == "select-multiple"){ if(typeof(myObj[name]) == 'undefined'){ myObj[name]=[ ]; } for (var jdx=0,len3=elm.options.length; jdx<len3; ++jdx){ if (elm.options[jdx].selected){ myObj[name].push(elm.options[jdx].value); } } } // if name=undefined; }); // forEach ***/ return obj;