4

All I was attempting was to edit some old code, where I had made an option group, but now I'd like to take it away. I can't just do it with css because it's using the chosen plugin.

function flavorChooser (title, item) {
var title = 'hidden';
var optgroup = jQuery('<optgroup display="' + title + '"/>');


jQuery.each(item, function(sub_title, sub_item) {
    if (typeof sub_item[0] !== "undefined") {
        var opt = jQuery('<option value="' + sub_item[0] + '" data-store-locator-value="' + sub_item[0] + '">' + sub_title + '</option>');
        optgroup.prepend(opt);
    }
});
// console.log('chozen ');
// jQuery('.chzn-container .chzn-results .group-result').css('display', 'none');
return optgroup;

}

4

3 回答 3

8

This works to remove all of the optgroup children of a given select element:

Markup:

<select id="mySelect">
    <optgroup label="MyGroup"></optgroup>
    <option>Option</option>
    <option>Option</option>
    <option>Option</option>
    <option>Option</option>
    <optgroup label="MyGroup2"></optgroup>
    <option>Option</option>
    <option>Option</option>
    <option>Option</option>
</select>

jQuery:

 $(function () {
        $("#mySelect").children().remove("optgroup");
    });

jsFiddle: http://jsfiddle.net/frJEq/

于 2013-02-19T22:16:36.040 回答
4

这是如何从标签中找到它并删除,

$("#mySelect").children().remove("optgroup[label='MyGroup']");

ps 这就是我想要的,为了他人的利益而出版

于 2013-09-02T09:42:23.080 回答
1

$("#mySelect").children().remove('optgroup[label=MyGroup2]');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<select id="mySelect">
	<optgroup label="MyGroup">
		<option>Option1</option>
		<option>Option2</option>
		<option>Option3</option>
		<option>Option4</option>
	</optgroup>
		<optgroup label="MyGroup2">
		<option>Option5</option>
		<option>Option6</option>
		<option>Option7</option>
	</optgroup>
</select>

于 2015-05-09T04:33:31.673 回答