使用dijit/form/CheckBox发现序列化的时候将他的值序列化为Array。不知道为啥?请大侠指教!
HTML源码如下
<!DOCTYPE html>
<head>
<Meta http-equiv="X-UA-Compatible" content="IE=7" />
<title></title>
<link href="dijit/themes/claro/claro.css" rel="stylesheet" type="text/css" />
<script src="dojo/dojo.js" data-dojo-config="async: true,parSEOnLoad: true"></script>
<script type="text/javascript">
require(["dojo/ready","dijit/Dialog","dijit/form/Button","dijit/form/CheckBox"],function(ready){
ready(function(){
});
});
</script>
</head>
<body class="claro">
<div data-dojo-type="dijit/Dialog" data-dojo-id="myFormDialog" title="Form Dialog" execute="alert('submitted w/args:\n' + dojo.toJson(arguments[0],true));">
<div class="dijitDialogPaneContentArea">
<table>
<tr>
<td><label for="EState">CheckBox</label></td>
<td><input id="EState" name="EState" data-dojo-id="frm_state" data-dojo-type="dijit/form/CheckBox" data-dojo-props="value:true" /></td>
</tr>
<tr>
<td><label for="EState1">CheckBox</label></td>
<td><input id="EState1" name="EState" data-dojo-id="frm_state" data-dojo-type="dijit/form/CheckBox" data-dojo-props="value:true" /></td>
</tr>
</table>
</div>
<div class="dijitDialogPaneActionBar">
<button data-dojo-type="dijit/form/Button" type="submit" data-dojo-props="onClick:function(){return myFormDialog.validate();}">
OK
</button>
<button data-dojo-type="dijit/form/Button" type="button" onClick="myFormDialog.hide()">
Cancel
</button>
</div>
</div>
<p>When pressing this button the dialog will popup:</p>
<button id="buttonThree" data-dojo-type="dijit/form/Button" type="button" onClick="myFormDialog.show();">
Show me!
</button>
</body>
</html>
经过调试发现
_DialogMixin.js中_onSubmit函数中
this.onExecute();
this.execute(this.get('value')); //这就是我们dijit/Dialog 的execute属性。this.get('value')
在_FormMixin.js的_getValueAttr函数中,_getValueAttr源码如下:
var obj = { };
array.forEach(this._getDescendantFormWidgets(),function(widget){
var name = widget.name;
if(!name || widget.disabled){ return; }
// Single value widget (checkBox,radio,or plain <input> type widget)
var value = widget.get('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){
lang.setObject(name,value,obj);
}else{
// give radio widgets a default of null
value = lang.getObject(name,false,obj);
if(value === undefined){
lang.setObject(name,null,obj);
}
}
}else{
// checkBox/toggle button
var ary=lang.getObject(name,obj); //这就是代码
if(!ary){
ary=[];
lang.setObject(name,ary,obj);
}
if(value !== false){
ary.push(value);
}
}
}else{
var prev=lang.getObject(name,obj);
if(typeof prev != "undefined"){
if(lang.isArray(prev)){
prev.push(value);
}else{
lang.setObject(name,[prev,value],obj);
}
}else{
// unique name
lang.setObject(name,obj);
}
}
});
为啥作为数组使用?还是我使用方法有问题?
上面的HTML源码使用两个CheckBox,返回的值如源码所示只有当true才显示否则不显示。
那么两个CheckBox只返回true而且还没有索引(否则不知道那个选true,那个选false)有啥用?