ExtJS为什么form.getValues()仅对复选框状态返回“1”,但不是“0”?
问题描述:
我在窗口内部有一个简单的ExtJS 4(xtype窗体)。它有两个字段和一个保存按钮。其中一个字段是一个字符串。这工作正常。如果它是空白的,getValues()会给我一个空白字段。如果设置了,我会得到这个值。但是,该复选框是不同的。如果检查,我会得到'1'的值。但是,如果我取消选中该框(或不检查它),我就没有任何价值 - 该字段未在getValues()的结果中列出!为什么??ExtJS为什么form.getValues()仅对复选框状态返回“1”,但不是“0”?
items: [
{
xtype: 'checkbox',
name : 'insuranceVerified',
fieldLabel: 'insuranceVerified',
inputValue:'1',
value:'1' //does not make a difference
},
{
xtype: 'textfield',
name : 'ru',
fieldLabel: 'ru'
}
]
我赶上保存按钮单击事件,并在我的控制器调用这个函数:
updateEncounter: function(button) {
console.log('clicked the Save button');
var win = button.up('window'),
form = win.down('form'),
record = form.getRecord(),
values = form.getValues();
// values properly shows the ru field on the form, but only shows insuranceVerified if it's checked - is not in values if it's unchecked... ??!
debugger;
record.set(values);
this.getEncountersStore().sync();
win.close();
}
我只是想出了别的东西,虽然这不是我一个完整的答案: 如果我改变上面的代码,以便得到它的形式(我从形式面板假设):
form = win.down('form').getForm(), // instead of win.down('form')
然后,使用getFieldValues()的新对象代替的GetValues(),我现在具有复选框值,即使它未被选中。
values = form.getFieldValues(); // instead of getValues()
不过,我回来的值是 '假'(或真),不是 '0' 或 '1' 像我inputValue将规定: '1'。
此外,我甚至尝试为字段'1'设置一个'值',无论我为getValues还是getFieldValues()做这个或没有区别。
答
Submit an HTML form with empty checkboxes
你有什么期望,如果没有选中该复选框接受?你指定的,如果它被选中正在发送的值..
这里另一种解决方案: How come checkbox state is not always passed along to PHP script?
答
可以使用
var answer1IsCorrect = Ext.getCmp('Answer1RadioButton').checked ? 1 : 0;
insted的的true
或false
答
这对我的作品。希望能帮到
{
xtype: 'checkbox',
boxLabel: modelFields[i].label,
name: modelFields[i].name,
//magic!
inputValue: 1,
uncheckedValue: 0
}
那么,在getFieldValues()的情况下,如果选中它,则返回true;否则返回false。我真正想要的是'1'(如果选中)和0(如果未选中)。所以根据你提供的链接,在我的PHP后端,除非它被发送,否则我将不得不假设该字段为'0'。我理解这些链接中的HTML/PHP问题,但ExtJS似乎应该将该字段标记为脏,并且不应该将其选中为ExtJS中的值,这些值是false,0,NO等,然后可以发送给服务器更新数据库? –
好的,在发布我的最后评论之后,我找到了答案,只需在复选框定义中添加一个属性:uncheckedValue:'0'! –