简体   繁体   English

在Sencha Touch中处理单选按钮上的事件

[英]Handling events on radio buttons in Sencha Touch

I'm trying to add a bit of code that gets called whenever the user clicks on one of the radio button options in a panel in Sencha touch. 我试图添加一些代码,只要用户单击Sencha touch面板中的单选按钮选项之一,就会调用这些代码。 I have it working by adding a click listener to the body of the panel and just checking the target and making sure it's a radio button (has a value). 我通过在面板的主体中添加一个单击侦听器并仅检查目标并确保它是一个单选按钮(具有一个值)来进行工作。

I'm sure there's a better way to bind this handler. 我确定有更好的方法绑定此处理程序。 I've watched some of the tutorials about listeners, and from what I understand "I'm doing it wrong". 我看了一些关于侦听器的教程,据我了解,“我做错了”。 But where can I find the info about doing it right? 但是在哪里可以找到有关正确执行操作的信息? Here's a stripped down version of what I'm doing, which works using Sencha Touch 1.1.0 (this is the app javascript file): 这是我正在做的精简版,可以使用Sencha Touch 1.1.0(这是应用javascript文件)运行:

Ext.setup({
    onReady: function() {
        panel = new Ext.Panel({
            fullscreen: true,
            title: 'Test',
            scroll: 'vertical',
            items: [{
                xtype: 'fieldset',
                defaults: {
                    xtype: 'radiofield',
                    labelWidth: '80%',
                    name: 'opt',
                },
                items: [{label: 'first', value: 1}, {label: 'second', value: 2}]
            }],
        });

        panel.addListener({
            body: {
                click: function(ctl) {
                    if (ctl && ctl.target && ctl.target.value) {
                        console.log('checked control ' + ctl.target.value);
                    }
                }
            }
        });
    }
});

I'm assuming what I should be doing is binding to the fieldset instead of the body, and probably listening for a change event instead of a click event. 我假设我应该做的是绑定到字段集而不是正文,并且可能正在侦听change事件而不是click事件。 But all the forms I've tried besides this one don't seem to work. 但是除此以外,我尝试过的所有形式似乎都行不通。

I've worked down a version that looks like this: 我已经制定了一个看起来像这样的版本:

panel = new Ext.Panel({
    fullscreen: true,
    title: 'Test',
    scroll: 'vertical',
    items: [{
        xtype: 'fieldset',
        defaults: {
            xtype: 'radiofield',
            labelWidth: '80%',
            name: 'opt',
        },
        items: [{label: 'first', value: 1}, {label: 'second', value: 2}]
    }],
    listeners: {
        el: {
            tap: function(ctl) {console.log("Checked " + ctl.target.value);},
            delegate: "input",
        }
    },
});

I suspect there's a way to attach that to the fieldset element instead of the panel, but this is a lot better than the alternatives I had. 我怀疑有一种方法可以将其附加到fieldset元素而不是面板上,但这比我的替代方法好得多。 I no longer have to inspect the target item passed into the click event handler to see if it was a radio button, the delegate setting takes care of that. 我不再需要检查传递给click事件处理程序的目标项目,看看它是否是单选按钮,委托设置就可以解决这一问题。 And now the handler works appropriately when I dynamically add items to the fieldset. 现在,当我将项目动态添加到字段集中时,处理程序将正常工作。

You can try adding listeners to your radiofield items, or like you said, to the fieldset instead. 您可以尝试将侦听器添加到您的无线电场项目中,或者像您说的那样添加到字段集。 This should work for radiofield items: 这应该适用于无线电场项目:

items: [{
    label: 'first',
    value: 1,
    listeners: {
        check: function(button) {
            console.log('...');
        }
    }
}, {
    label: 'second',
    value: 2,
    listeners: {
        check: function(button) {
            console.log('...');
        }
    }
}]

You can add the listener(s) to the default section of the original script, like so: 您可以将侦听器添加到原始脚本的默认部分,如下所示:

xtype: 'radiofield',  
        listeners: {  
            check: function() {   
            alert(this.value);  
            }  
        },  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM