最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript solution for hiding dropdown list options - Stack Overflow

programmeradmin2浏览0评论

Edit: Thanks everybody, but nothing seems to work. I am inserting this code in a file that I know is being used and that contains other javascript blocks normally formatted, and this still doesn't work. It works in a fiddle, but not on my code. I guess this is too specific to the platform and extension that I'm trying to modify (this is part of a Magento checkout step modified by a third party extension). I will start looking into replacing the list with a manually generated one. Thanks again.


I am trying to hide an option in a dropdown list that is dinamically generated. The CSS solution doesn't work on all browsers, and even though I have found several similar questions here, neither one offers a solution that works for me.

Here's what my list renders like:

<select id="timeselect" name="adj[delivery_time][]" title="El plazo de la entrega" class="adjtimeselect select" type="time" ><option id="option-10" value="10" >10</option>
<option id="option-11" value="11" >11</option>
<option id="option-12" value="12" >12</option>
<option id="option-13" value="13" >13</option>
<option id="option-14" value="14" >14</option>
<option id="option-15" value="15" >15</option>
<option id="option-16" value="16" >16</option>
<option id="option-17" value="17" >17</option>
<option id="option-18" value="18" >18</option>
<option id="option-19" value="19" >19</option>
<option id="option-20" value="20" >20</option>
</select> 

I need to hide the option with value "12" for example. I am using this JS:

$("#timeselect option[value='12']").remove();

Any advice would be greatly appreciated since I'm new to JS.

Thanks.

Edit: Thanks everybody, but nothing seems to work. I am inserting this code in a file that I know is being used and that contains other javascript blocks normally formatted, and this still doesn't work. It works in a fiddle, but not on my code. I guess this is too specific to the platform and extension that I'm trying to modify (this is part of a Magento checkout step modified by a third party extension). I will start looking into replacing the list with a manually generated one. Thanks again.


I am trying to hide an option in a dropdown list that is dinamically generated. The CSS solution doesn't work on all browsers, and even though I have found several similar questions here, neither one offers a solution that works for me.

Here's what my list renders like:

<select id="timeselect" name="adj[delivery_time][]" title="El plazo de la entrega" class="adjtimeselect select" type="time" ><option id="option-10" value="10" >10</option>
<option id="option-11" value="11" >11</option>
<option id="option-12" value="12" >12</option>
<option id="option-13" value="13" >13</option>
<option id="option-14" value="14" >14</option>
<option id="option-15" value="15" >15</option>
<option id="option-16" value="16" >16</option>
<option id="option-17" value="17" >17</option>
<option id="option-18" value="18" >18</option>
<option id="option-19" value="19" >19</option>
<option id="option-20" value="20" >20</option>
</select> 

I need to hide the option with value "12" for example. I am using this JS:

$("#timeselect option[value='12']").remove();

Any advice would be greatly appreciated since I'm new to JS.

Thanks.

Share Improve this question edited Aug 18, 2013 at 16:27 Palmer Del Campo asked Aug 18, 2013 at 15:38 Palmer Del CampoPalmer Del Campo 1392 gold badges3 silver badges11 bronze badges 13
  • 4 Try using $("#timeselect option[value='12']").hide(). For further reading: stackoverflow.com/questions/11071562/remove-option-in-select – Itay Commented Aug 18, 2013 at 15:41
  • .remove() is the right function you are using... whats problem in it.. you just want to hide not to remove??? – maverickosama92 Commented Aug 18, 2013 at 15:42
  • Note that $ indicates jQuery, so you need to include that before your script. – Jens Neubauer Commented Aug 18, 2013 at 15:47
  • @maverickosama92 I can do with either hide or remove, I just need it to be impossible to select via any possible method, but either way nothing happens, the option is still there. – Palmer Del Campo Commented Aug 18, 2013 at 15:49
  • Then you're probably missing something... Maybe the jQuery include like people said here. Please upload your code to jsfiddle.net so we can take a look – Itay Commented Aug 18, 2013 at 15:53
 |  Show 8 more comments

4 Answers 4

Reset to default 5

Use the hide() function of JQuery: jsFiddle

You can use show() to get it back

Jquery remove by value

$("#timeselect option[value=11]").remove();

Jquery remove by Text

$("#timeselect option:contains(11)").remove();

Jquery to hide a select box option with its value using css

   $("#timeselect option[value='11']").hide();

or

   $("#timeselect option[value='11']").css('display','none');

I tried in many different ways but this solution seems reasonable and cross browser compatible and I have used in my code. No plugins required simple register function with jquery object

Solution at glance:

(function ($) {


$('#showOne').click(function () {
    $('#ddlNumbers').showHideDropdownOptions('3', true);
});

$('#hideOne').click(function () {
    $('#ddlNumbers').showHideDropdownOptions('3', false);
});

 $.fn.showHideDropdownOptions = function(value, canShowOption) { 

         $(this).find('option[value="' + value + '"]').map(function () {
            return $(this).parent('span').length === 0 ? this : null;
        }).wrap('<span>').hide();

        if (canShowOption) 
            $(this).find('option[value="' + value + '"]').unwrap().show();
        else 
            $(this).find('option[value="' + value + '"]').hide();

}



})(jQuery);

Here is the complete implementation http://jsfiddle.net/8uxD7/3/

You can use jQuery remove() function. if you want to remove it permanently from DOM, or if you want to remove and reinsert it use detach()

$("#timeselect option[value='12']").remove();

Or Detach

var value = $("#timeselect option[value='12']").detach();

And reinsert it by using

$("#timeselect").append(value);

http://jsbin.com/iHIrAQE/5/edit

See the example

Another way is you can disable the value so user can see but cannot select

$("#timeselect option[value='12']").attr('disabled','disabled');
发布评论

评论列表(0)

  1. 暂无评论