简体   繁体   English

如何从Jquery中的多选下拉列表中删除选项

[英]How to remove Options from multiselect dropdown in Jquery

I have some table column with some preselected values and now i want to remove those selected values from dropdown(ddlMultiselect) .. Both table column and dropdown option values are same and what i want that those values should be hide/remove from dropdown as per if condition. 我有一些带有一些预选值的表列,现在我想从下拉列表中删除这些选定的值(ddlMultiselect)。表列和下拉选项值都是相同的,我希望这些值应该从下拉列表中隐藏/删除如果条件。

$('#sometabletr:gt(0)').each(function () {
            var row = $('td:eq(0) > span', this).text();
            $('#ddlMultiselect :selected').each(function () {
                var col = $(this).val();
                if (row == col) {
                    $(this).remove();
                 }
            });
        });

This is the way is do it, fast and easy way 这是做到这一点的方式,快捷方式

            $('#listname option:selected').each(function (index, option) { 
                $(option).remove(); 
            });      

There is another way to approach this issue.. but setting up classes on the table rows, all you have to do is change the class of the table element itself to hide/show vast amounts of things while only doing a single repaint, which GREATLY improves performance. 还有另一种解决这个问题的方法..但是在表行上设置类,你所要做的就是改变表元素本身的类来隐藏/显示大量的东西,同时只进行一次重绘,这非常棒提高性能。

In this example, I have the adding of a class hard-coded, but you could use jQuery's addClass and removeClass or look up the best alternatives available. 在这个例子中,我添加了一个硬编码的类,但你可以使用jQuery的addClassremoveClass或者查找可用的最佳替代方案。

<doctype html>
<html>
   <header>
      <title>Demo HIde</title>

      <style>
      #mytable.even tr.odd {
        display:none;
      }   
      </style>


   </header>
   <body>


   <table id="mytable">
   <tr class="odd"><td>1</td></tr>
   <tr class="even"><td>2</td></tr>
   <tr class="odd"><td>3</td></tr>
   <tr class="even"><td>4</td></tr>
   <tr class="odd"><td>5</td></tr>
   <tr class="even"><td>6</td></tr>

   </table>

   <script>
   // Show the even values only
   document.getElementById("mytable").className += " even";
   </script>


   </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM