Jquery从下拉列表中删除项目 [英] Jquery remove item from dropdownlist

查看:94
本文介绍了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;
        }
    }
});

这篇关于Jquery从下拉列表中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆