如何访问除选择的选项之外的选项的每个选项?选择除选定的选项以外的每个选项
我想,但没有成功:
$('select[id^="buyingSupplierRef"]').live('mouseleave', function()
{
$(this+' option:not(option:selected)').each(function(){
$(this).detach() ;
})
如何访问除选择的选项之外的选项的每个选项?选择除选定的选项以外的每个选项
我想,但没有成功:
$('select[id^="buyingSupplierRef"]').live('mouseleave', function()
{
$(this+' option:not(option:selected)').each(function(){
$(this).detach() ;
})
尝试以下操作:
$('option:not(:selected)', this).each(function(){
// ...
})
注意live()
已被弃用,你可以使用on()
代替。
您不能使用this + ' ...'
来做到这一点,this
是一个DOM元素。相反,使用find
:
$(this).find('option:not(:selected)').each(function(){ ...
(你的选择,option:not(option:selected)
是很好的,你不有重复option
位,它没有任何效果,因此以上。)
$("option:not(:selected)", this).detach();
无论如何,没有必要对.each
,.detach
进行操作。
孩子()会更好 – bugwheels94 2012-07-25 08:35:08
@AnkitGautam:或者,它并不重要。等等,实际上它确实:'option'元素可以在['optgroup'元素](http://www.w3.org/TR/html5/the-optgroup-element.html#the-optgroup-element) ,在这种情况下'孩子'会失败,但'find'会起作用。 – 2012-07-25 08:37:53