jQuery jQuery在带有空/空值的Select中查找选项将其删除并使用值选择它之后的第一个选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11022502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:06:21  来源:igfitidea点击:

jQuery find option in Select with empty/null value remove it and select the first option after it with a value

jqueryforms

提问by chris

All in all I want to remove any and all empty options from a SELECT input. The select is dynamically generated from a script someone else wrote, based on an object it receives. My problem is sometimes the object has empty values, leaving me with a select option set that leaves something to be desired.

总而言之,我想从 SELECT 输入中删除任何和所有空选项。选择是根据其他人编写的脚本根据它接收到的对象动态生成的。我的问题是有时对象有空值,给我留下一个选择选项集,留下一些需要的东西。

Anyway almost every time the first option in the set is empty. So I want to remove that and apply selected="selected"to the first option that comes up after the first one is removed., while removing any other possibilities that have empty values.

无论如何,几乎每次集合中的第一个选项都是空的。所以我想删除它并应用于selected="selected"第一个被删除后出现的第一个选项,同时删除任何其他具有空值的可能性。

So in testing the initial concept I came up with

所以在测试最初的概念时我想出了

$(select).each(function(){
                if ($(this+' option:selected').val() == '')
                {
                    //this assumes that your empty value is ''
                    $(this).remove();
                }
            });

which seems to remove the entire select, and I know its cause thisis defined as the select itself.

这似乎删除了整个选择,我知道它的原因this被定义为选择本身。

I've also tried $(select +'option[value=""]').remove();

我也试过 $(select +'option[value=""]').remove();

Currently I am only trying to get it to remove the options with the empty values, after I can achieve that then I will figure out how I to select the first item on the list officially as far as the DOM would be concerned.

目前我只是试图让它删除带有空值的选项,在我可以实现之后,我将弄清楚如何就DOM而言正式选择列表中的第一项。

also worth mentioning 'select' the variable being passed is defined previously in the function so it is working for that part of the notion. Just sayin so no one thinks I am some how confusing it with something else.

还值得一提的是,“选择”传递的变量是先前在函数中定义的,因此它适用于概念的那部分。只是说,所以没有人认为我是如何将它与其他东西混淆的。

回答by Didier Ghys

  • you can use .filter()to filter out the empty option elements and remove them all at once

  • use .prop()to set special attributes like selected

  • 您可以使用.filter()过滤掉空的选项元素并一次性删除它们

  • 使用.prop()设置特殊属性,如selected

Code:

代码:

$('select option')
    .filter(function() {
        return !this.value || $.trim(this.value).length == 0;
    })
   .remove();

$('select option')
    .first()
    .prop('selected', true);?

DEMO

演示

回答by mangas

This can be done easier:

这可以更容易地完成:

Element.find('option[value=""]').remove();

回答by Travis J

POJS to the rescue!

POJS 来救援!

function fixSelectOptions(SelectId){
 var sel = document.getElementById(SelectId);
 for(var i = 0; i < sel.options.length; i++){
  if(sel.options[i].value === ""){
   sel.remove(i--);//decrease i to preserve future index references after removal
  }
 }
 sel.options.selectedIndex = 0;
}