2017-03-22 34 views
0

我使用select2来构建我的组合。select2,如何更改前显示

一些元素是残疾人,但我想告诉他们大胆但ARIA-需要= “假”

var data= [{ 
    id="1", 
    value = "first", 
    disabled:true 
},{ 
    id="2", 
    value = "second", 
    disabled:false 
}, 

]; 
function formatResult(node) { 
    if(node.disabled){ 
    var $result = $('<span><strong>' + node.text + '</strong></span>'); 
    } else { 
    var $result = $('<span>' + node.text + '</span>'); 
    } 
    return $result; 
} 

$("#myCombo").select2({ 
    placeholder: 'Seleccione una opción', 
    width: "350px", 
    data: data, 
    formatSelection: function(item) { 
    return item.text 
    }, 
    formatResult: function(item) { 
    return item.text 
    }, 
    templateResult: formatResult 
}); 

};

这样的作品,但李:

<li class="select2-results__option" role="treeitem" aria-disabled="true"> 
     <span style="padding-left:20px;"><strong>first</strong></span> 
</li> 

当它打开时,与调试,如果我执行:

$('.select2-results__option').attr('aria-disabled',false); 

它的作品,因为我想,但我不能这样做这在编程上,似乎没有存在一个beforeShow函数,我该怎么做?

回答

1

通常,您可以通过超时实现此目的。

$("#myCombo").select2({ 
    placeholder: 'Seleccione una opción', 
    width: "350px", 
    data: data, 
    formatSelection: function(item) { 
    return item.text 
    }, 
    formatResult: function(item) { 
    return item.text 
    }, 
    templateResult: formatResult 
}); 
setTimeout(function(){ 
    $('.select2-results__option').attr('aria-disabled',false); 
}); 
+0

它的工作原理,但我应该把setTimeout放在我的formatResult函数中,因为每次我点击select – cucuru