获取具有特定类名的所有选择下拉列表的正确 jquery 选择器是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2064626/
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 12:41:53  来源:igfitidea点击:

What is the correct jquery selector to get all select dropdowns with a certain class name?

jqueryselectdrop-down-menuselector

提问by leora

i want to loop through all dropdown selects with a certain class name and add an item to it and i am just struggling with the correct selector

我想遍历所有具有特定类名的下拉选择并向其添加一个项目,我只是在为正确的选择器而苦苦挣扎



EDIT:I must be doing something wrong as most of the upvoted accepted answer dont seem to work so i think there must be some quirk in my code. I have pasted both the HTML and the jquery code below. let me know if this makes sense.

编辑:我一定是做错了什么,因为大多数赞成的答案似乎都不起作用,所以我认为我的代码中一定有一些怪癖。我已经粘贴了下面的 HTML 和 jquery 代码。让我知道这是否有意义。



HTML:

HTML:

<select onfocus="this.enteredText='';" onkeydown="return handleKey();" onkeyup="event.cancelbubble=true;return false;" onkeypress = "return selectItem();"  class="componentSelect"  id="components0" name="applicationUpdater.dependencies[0].componentName" >
<option value= 5 >Client</option>
<option value= 79 >Server</option>
</select>

 <select onfocus="this.enteredText='';" onkeydown="return handleKey();" onkeyup="event.cancelbubble=true;return false;" onkeypress = "return selectItem();"  class="componentSelect"  id="components1" name="applicationUpdater.dependencies[0].componentName" >
<option value= 5 >Client</option>
<option value= 79 >Server</option>
</select>

etc . . .

等等 。. .

jquery code:

jQuery代码:

    $('select.componentSelect').each(function() {
        var select = $(this);
        $(select).children('option').each(function() {
            if ($(this).text() == currentComponentName) {
                $(this).remove();
            }
        });

    });

采纳答案by SLaks

To answer your new question, you can use the following line to remove all <option>s containing a currentComponentName:

要回答您的新问题,您可以使用以下行删除所有<option>包含 a 的 s currentComponentName

$('select.componentSelect option:contains("' + currentComponentName + '")').remove();

Demo

演示

回答by Aron Rotteveel

You should use the .class selector.

您应该使用.class 选择器

// select every <select> element with classname "yourclassname"
$('select.yourclassname').each(function() {
    // $(this) now refers to one specific <select> element
    // we append an option to it, like you asked for
    $(this).append(
        $('<option value="foo">Bar</option>');
    );
});

For more information about how to properly select elements with jQuery, take a look at http://docs.jquery.com/Selectors.

有关如何使用 jQuery 正确选择元素的更多信息,请查看http://docs.jquery.com/Selectors

回答by Michael Waterfall

Give this a shot:

试一试:

$('select.className').each(function() {
   var currentSelect = $(this);
   // ... do your thing ;-)
});

回答by SLaks

You need to use the .classNameselector, like this:

您需要使用.className选择器,如下所示:

$('select.className')

This selector combines the elementselector (matching all <select>elements) with the className selector (matching all elements that contain the classNameclass) to find all <select>elements with that class.

此选择器将元素选择器(匹配所有<select>元素)与 className 选择器(匹配包含className该类的所有<select>元素)结合起来,以查找具有该类的所有元素。

回答by rosscj2533

Your posted jQuery code works for me, as long as I define currentComponentNameto be one of the options of your select. Aron Rotteveel's answer is very close, but the append part is a bit off, try this:

您发布的 jQuery 代码对我有用,只要我定义currentComponentName为您选择的选项之一。Aron Rotteveel 的回答非常接近,但附加部分有点不对,试试这个:

$('select.componentSelect').each(function() { 
    $(this).append('<option value="foo">Bar</option>'); 
});