验证组合框中的值是否存在或不存在?

时间:2015-06-25 13:49:01

标签: javascript extjs

如何验证组合框中列出的所有项目?例如,我想检查一个组合框是否有" item1" " ITEM2" "项目3&#34 ;.如何输出所有选项并检查?

以下是如何超出组合框:

Ext.ComponentQuery.query('combobox[name=boxname]')[0];

2 个答案:

答案 0 :(得分:0)

只需访问您的盒子的商店并询问它。例如:

var store = Ext.ComponentQuery.query('combobox[name=boxname]')[0].getStore();
console.log(store.getById('item1'));

如果item1不存在,则结果为null

更新:

  

根据条件可以说,我希望能够验证组合   它只有“item1”和“item2”的盒子,不再是或者更少。

假设变量target包含你想要的组合,你可以用这种方式验证你的组合:

var target = ['item1', 'item2'],
    combo = Ext.ComponentQuery.query('combobox[name=boxname]')[0],
    check = function(combo, target) {
        var store = combo.getStore();
        if (target.length !== store.getTotalCount()) {
            return false;
        }
        var result = true;
        store.each(function(item){
            if (!Ext.Array.contains(target, item.getId())) {
                result = false;
                return false;
            }
        });
        return result;
    };
console.log(check(combo, target));

如果组合包含您想要的内容,则check方法将返回true,否则将返回false

答案 1 :(得分:0)

var store = Ext.ComponentQuery.query('combobox[name=boxname]')[0].getStore();

store.each(function(record){
    var name = rec.get('name');// instead of name use the attribute you want to use for the validation
    if(name === 'code4jhon'){
         console.log('this dudes name is code4jhon')
     }

});

http://docs.sencha.com/extjs/4.2.3/#!/api/Ext.data.Store-method-each