2014-02-05 173 views
2

是否可以防止在选择多重控制中取消选择选项?此外,它不得取消选择任何其他选定的选项。我曾尝试过:jQuery - 防止在选择多个选项中取消选择

$('#selections option').click(function(){ 
    $(this).prop('selected', true); 
}); 

似乎不起作用。任何人都知道我可以做到这一点?

+0

选项一般不触发鼠标事件的一种方式,所以我会说不? – adeneo

回答

0

已经尝试过这个?:

$('#selections option').click(function(evt){ 
    evt.preventDefault(); 
    $(this).prop('selected', true); 
}); 
+0

如果您提出这个解决方案,那么建议它而不是质疑它。并解释为什么这是通过编辑答案。 –

1

这里做

$('#selections').on('change', function(e) { 
    var self = this, 
     selected = $(this).data('selected') || []; 

    $.each(selected, function(_,i) { 
     $('option', self).eq(i).prop('selected', true) 
    }); 

    $(this).data('selected', $.map($('option:selected', this), function(el) { 
     return $(el).index(); 
    })); 
}); 

FIDDLE

相关问题