开发者

Jquery remove item from dropdownlist

开发者 https://www.devze.com 2023-03-27 11:12 出处:网络
jQuery(document).ready(function () { $lstAccountType = $(\'select[id*=\"account_type\"]\'); $lstAccountType.change(function () {
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 cl开发者_JS百科icked. Does anyone have any pointers in regards to this?


You should just be able to use:

$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?

EDIT

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.

EDIT

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


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

should do the trick, if you're sure you always want to remove the first.

edit:

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

should do it on text.


Or if you want something easier to read, remember that you probably want to bind onFocus since you want to remove the first list-item as soon as the select is clicked.

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

精彩评论

暂无评论...
验证码 换一张
取 消