Javascript 如何获取复选框的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1941461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:59:58  来源:igfitidea点击:

How to get the value of checkbox

javascriptextjs

提问by xrx215

How to get the value of a check box?

如何获取复选框的值?

var tb = new Ext.Toolbar();

tb.add({
                xtype: 'checkbox',
                boxLabel: 'Expand Groups by Default',
                id: 'GetChkBoxValue',
                checked: true,
                handler: function() {
                    alert(this.getValue())
                }
       });

Is it possible to get the value of the checkbox outside tb.I have done something like this but it is not firing

是否有可能在 tb 之外获取复选框的值。我做了这样的事情,但它没有触发

Ext.getCmp('GetChkBoxValue').getValue(); 

采纳答案by xrx215

Here is what worked for me:

这是对我有用的:

var expandAllGroupsCheckbox = Ext.create(

            {
                xtype: 'checkbox',
                boxLabel: 'Expand Groups by Default',
                id: 'chkid',
                checked: true,
                afterRender: function() {
                    Ext.form.Checkbox.superclass.afterRender.call(this);
                    alert(this.getValue());// giving true 
                    this.checkChanged();
                },
                checkChanged: function()
                {
                    var checked = expandAllGroupsCheckbox.getValue();
                    gv.startCollapsed = !checked;
                    if ( gv.mainBody )
                    {
                        gv.toggleAllGroups( !checked );
                    }
                },
                listeners: {
                    check: {
                        fn: function(){expandAllGroupsCheckbox.checkChanged()}
                    }
                }

            });

And then:

进而:

tbar: [
               expandAllGroupsCheckbox
,
                    {
                        xtype: 'tbbutton',
                        icon: '/images/icon_list_props.gif',
                        handler: function() {
                            ShowPreferencesPage();
                        }
                    }
],

回答by Radu Brehar

Try out the following code, it should work:

试试下面的代码,它应该可以工作:

new Ext.Window({
    renderTo: Ext.getBody(),
    width: 500,
    height: 200,
    title: 'test window',
    items: [{
       xtype: 'checkbox',
       boxLabel: 'Expand Groups by Default',
       id: 'chkid',
       checked: true
    }]
}).show()
Ext.getCmp('chkid').getValue()

Then play with the checkbox and with getValue() you get its state (checked or not). Happy ExtJS coding!

然后使用复选框并使用 getValue() 获取其状态(已选中或未选中)。快乐的 ExtJS 编码!

回答by meh

Just this:

只是这个:

var cbox = Ext.getCmp('cboxId');
alert(cbox.checked);