admin管理员组文章数量:1420530
<select>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
</select>
The value which has been selected should not be appeared in the drop down. For instance, if i select "feb", Feb shouldn't appear in dropdown.
jsfiddle link: /
<select>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
</select>
The value which has been selected should not be appeared in the drop down. For instance, if i select "feb", Feb shouldn't appear in dropdown.
jsfiddle link: http://jsfiddle/jucLsmjx/
Share Improve this question edited Aug 4, 2015 at 3:56 Roko C. Buljan 207k41 gold badges328 silver badges340 bronze badges asked Aug 4, 2015 at 2:56 ShawnShawn 511 silver badge2 bronze badges 2- you forgot to close the last option tag properly. – Mike -- No longer here Commented Aug 4, 2015 at 2:59
- possible duplicate of Hide options in a select list using jQuery – Mohd Abdul Mujib Commented Aug 4, 2015 at 3:31
3 Answers
Reset to default 2$('#mySelect').on("change", function(){
$('option:selected', this).hide().siblings().show();
});
Additionally if you want to trigger the Option Hide right from the start, add .trigger('change');
:
$('#mySelect').on("change", function(){
$('option:selected', this).hide().siblings().show();
}).trigger('change');
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="mySelect">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
Your selector option[value=' + optionval + ']')
is wrong. You are trying to select the option by its value but value='XXX'
attribute is missing, so try this:
http://jsfiddle/jucLsmjx/8/
$('#mySelect').change(function(){
var optionval = $('#mySelect').val();
$('#mySelect option:contains("'+optionval+'")').hide().siblings().show();;
});
Just clone it(select element) and store it inside a variable
var $original = $("#mySelect").clone(true); // The argument "true" copies any event handlers.
Then go on and remove the selected <option>
from the <select>
$("#mySelect").change( function(e){
e.preventDefault();
var val = $(this).val();
$("#mySelect option[value='"+val+"']").remove();
});
The cloning part was for the case where you needed the original DOM element, then you can always append it to the DOM.
本文标签: javascriptHide the selected option in select dropdown listStack Overflow
版权声明:本文标题:javascript - Hide the selected option in select dropdown list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745337062a2654089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论