最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding a listener to create a warning message to a textfield - Stack Overflow

programmeradmin2浏览0评论

I have a bo box and a textfield. I am trying to Add a warning notifcation to the textfield stating 'please enter value' if something is selected from the bo box. However, I only want the warning to display on the textfield if '3' is selected from the bo box

Is there any way that I could do that? I am not sure how to do that.

Here is the code for my bobox and textfield:

{
                            xtype:'bo',
                            store: ['1','2','3'],
                            triggerAction: 'all',
                            fieldLabel: 'Area',
                            id: 'dcArea',
                            width: 125,
                        },{
                            xtype:'textfield',
                            fieldLabel: 'Mile',
                            id: 'mile',
                            width: 125,
                        }

I have a bo box and a textfield. I am trying to Add a warning notifcation to the textfield stating 'please enter value' if something is selected from the bo box. However, I only want the warning to display on the textfield if '3' is selected from the bo box

Is there any way that I could do that? I am not sure how to do that.

Here is the code for my bobox and textfield:

{
                            xtype:'bo',
                            store: ['1','2','3'],
                            triggerAction: 'all',
                            fieldLabel: 'Area',
                            id: 'dcArea',
                            width: 125,
                        },{
                            xtype:'textfield',
                            fieldLabel: 'Mile',
                            id: 'mile',
                            width: 125,
                        }
Share Improve this question asked Sep 18, 2013 at 16:25 user1676428user1676428 6711 gold badge7 silver badges13 bronze badges 1
  • I don't usually work with extJS, so I'd suggest binding an event handler / listener to your textfield for the change event. Then check if that bobox holds the value 3. If so, display your warning. Maybe there's an easier solution with extJS, but idk. – Kiruse Commented Sep 18, 2013 at 16:28
Add a ment  | 

2 Answers 2

Reset to default 2

The listener needs to be on the bo, of course. Use msgTarget to customize how the error message is displayed.

{
    xtype:'bo',
    store: ['1','2','3'],
    triggerAction: 'all',
    fieldLabel: 'Area',
    // bad practice
    // id: 'dcArea',
    width: 150,

    // <EDIT>
    msgTarget: 'under',
    // </EDIT>

    listeners: {
        change: function(bo, value) {
            // use ponent query to retrieve the other field
            var textfield = this.up('form').down('#mile');
            if (value === '3') {
                textfield.markInvalid("Please enter value");
            } else {
                textfield.clearInvalid();
            }
        }
    }
},{
    xtype:'textfield',
    fieldLabel: 'Mile',
    itemId: 'mile',
    width: 150
}

Edit Use the msgTarget property of the bo box to configure how the error message is displayed (see the updated code example).

In extjs this is the way to add listeners, on change will be helpful in this case. Sample:

              {
                    xtype:'textfield',
                    fieldLabel: 'Mile',
                    id: 'mile',
                    width: 125,
                    name: 'myMiles'
                    listeners: {
                         'change': function() {
                          console.log('you changed the text of this input field');
                          var value = Ext.getCmp('mile').getValues().myMiles;
                          if(value == 3)
                          {
                            alert('You selected 3');
                          }
                        }
                    }
              }
发布评论

评论列表(0)

  1. 暂无评论