ExtJS为什么form.getValues()只返回“1”,但对于复选框状态不返回“0”?

时间:2011-11-28 13:59:52

标签: forms extjs checkbox

我在窗口内有一个简单的ExtJS 4(xtype表单)。它有两个字段和一个保存按钮。其中一个字段是字符串。这很好。如果它是空白的,则getValues()会将该字段留空。如果设置了,我就得到了价值。但是,复选框不同。如果选中它,我会得到'1'的值。但是,如果我取消选中该框(或不检查它),我没有得到任何价值 - 该字段未列在getValues()的结果中! WHY ??

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')

然后,在新对象而不是getValues()上使用getFieldValues(),我现在有了复选框值,即使它未被选中。

values = form.getFieldValues();  // instead of getValues()

但是,我得到的值是'false'(或true),而不是'0'或'1',就像我用inputValue指定的那样:'1'。

另外,我甚至尝试为字段“1”设置“值”,无论我是否为getValues或getFieldValues()执行此操作都没有区别。

3 个答案:

答案 0 :(得分:6)

这对我有用。希望能有所帮助

{
   xtype: 'checkbox',
   boxLabel: modelFields[i].label,
   name: modelFields[i].name,
   //magic!
   inputValue: 1,
   uncheckedValue: 0
}

答案 1 :(得分:0)

Submit an HTML form with empty checkboxes

如果未选中此复选框,您希望收到什么?如果选中它,则指定要发送的值。

这是另一个解决方案: How come checkbox state is not always passed along to PHP script?

答案 2 :(得分:-1)

你可以使用

var answer1IsCorrect = Ext.getCmp('Answer1RadioButton').checked ? 1 : 0;

而不是truefalse