2011-08-10 36 views
2
jQuery(document).ready(function() { 

    $lstAccountType = $('select[id*="account_type"]'); 

    $lstAccountType.change(function() { 
    $(this).remove('option[text="select one"]') 
    }); 

}); 

我想删除下拉列表中的第一个元素,当它被点击时。有没有人有任何关于这方面的指针?Jquery从下拉列表中删除项目

+0

这一个为我工作。不是其他的($(“#list [value ='elementid']”)。remove());谢谢 –

回答

7

你应该只能够使用:

$lstAccountType.change(function() { 
    $lstAccountType.find("option[value='"+$lstAccountType.val()+"']").remove(); 
}); 

我没有测试过这一点,但一展身手,让我知道,如果它是任何好还是不好?

编辑

如果你只是想删除的第一个选项,每次你可以尝试:

$lstAccountType.change(function() { 
    if ($lstAccountType.find("option:first").attr("value") == $lstAccountType.val()) { 
     $lstAccountType.find("option[value='"+$lstAccountType.val()+"']").remove(); 
    } 
}); 

它需要一定的整理,但希望这可以帮助。

编辑

如果你想删除只有一次,你可以做第一个选项:

var first_option_removed = false; 
$lstAccountType.change(function() { 
    if (!first_option_removed) { 
     if ($lstAccountType.find("option:first").attr("value") == $lstAccountType.val()) { 
      $lstAccountType.find("option[value='"+$lstAccountType.val()+"']").remove(); 
      first_option_removed = true; 
     } 
    } 
}); 
+0

想过它会删除选中的每个选项,我想象的不是你在找什么。我会更新我的答案。 –

0
$(this).remove('option:first') 

应该做的伎俩,如果你确定你总是希望删除第一个。

编辑:

$(this).remove('option:contains(select one)'); 

应该这样做文本。

+0

我想根据文本删除 – JBone

0

或者,如果你想要的东西更容易阅读,请记住,你可能要绑定onFocus自你想删除第一个列表项,只要选择被点击

$('#mySelectId').bind('focus' function(){ 
    var listItems = $('#mySelect li'); 
    if(listItems.length > 0){ 
     $(listItems[0]).remove(); 
    } 
}); 
相关问题