Jquery从下拉列表中删除项目

问题描述:

jQuery(document).ready(function () { 

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

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

});

我想删除点击下拉列表中的第一个元素。有人有任何指针吗?

I want to remove the first element in my dropdown list when it is clicked. Does anyone have any pointers in regards to this?

你应该可以使用:

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

我还没有测试过这个,但是去看看,如果有什么好处,不是?

I haven't tested this, but have a go and let me know if it is any good or not?

编辑

如果您只想删除第一个选项时间可以尝试:

If you only want to remove the first option each time you could try:

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

需要一些整理,但希望可能有所帮助。

It needs some tidying up, but hopefully that might help.

编辑

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

If you want to remove the first option only once you could do:

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;
        }
    }
});