Jquery从下拉列表中删除项目

JBo*_*one 2 jquery drop-down-menu

jQuery(document).ready(function () { 

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

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

});
Run Code Online (Sandbox Code Playgroud)

我想在单击时删除下拉列表中的第一个元素.有没有人对此有任何指示?

Ada*_*cey 7

你应该只能使用:

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

我没有对此进行过测试,但是如果它有任何好处,请告诉我.

编辑

如果您只想在每次尝试时删除第一个选项:

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

它需要一些整理,但希望这可能会有所帮助.

编辑

如果您只想删除第一个选项,请执行以下操作:

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;
        }
    }
});
Run Code Online (Sandbox Code Playgroud)