根据按钮的处理程序条件

时间:2015-09-23 06:21:42

标签: javascript extjs

如何在按钮的处理程序中添加if / else条件。因此,如果条件为真,我将按钮指向某个函数(提交),否则指向另一个函数(submit1)。

var btn_submitform = Ext.create('Ext.Button', {
            width:75,
            height:30,
            text:'Submit', 
            id:"btn_submitform",
            handler: submit
    });

1 个答案:

答案 0 :(得分:1)

只需在处理程序中检查该条件:

onSubmit: function(button, event, eOpts) {
    if(true) {
        this.function1();
        //
        this.fireEvent('event1', [button, event, eOpts]);
    }
    else {
        this.function2();
        //
        this.fireEvent('event2', [button, event, eOpts]);
}

或基于该条件绑定处理程序(例如在控制器中):

var button = Ext.ComponentQuery.query('button[id=btn_submitform]')[0];

if(true) {
    button.on('click', 'function1');
}
else {
    button.on('click', 'function2');
}

或创建委托:

var form = Ext.ComponentQuery.query('form')[0];

form.addListener({
    // Ext.Buttons have an xtype of 'button', so we use that are a selector for our delegate
    delegate: 'button',

    click: function(button, event, eOpts) {
        if (button.id === 'btn_submitform') {
            alert('Submit button tapped!');
        }
        else {
            alert('Other button tapped!');
        }
    }
});