有没有办法删除optgroup而不破坏代码? [英] Is there a way to remove optgroup without destroying the code?

查看:309
本文介绍了有没有办法删除optgroup而不破坏代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所尝试的是编辑一些旧的代码,在那里我做了一个选项组,但现在我想把它删掉。我不能只是使用css,因为它使用所选择的插件。

  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;

}

解决方案

这可以删除给定 select 元素的所有 optgroup 子项:



标记:

 < select id =mySelect> 
< optgroup label =MyGroup>< / optgroup>
< option>选项< / option>
< option>选项< / option>
< option>选项< / option>
< option>选项< / option>
< optgroup label =MyGroup2>< / optgroup>
< option>选项< / option>
< option>选项< / option>
< option>选项< / option>
< / select>

jQuery:

 code> $(function(){
$(#mySelect)。children()。remove(optgroup);
});

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


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;

}

解决方案

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/

这篇关于有没有办法删除optgroup而不破坏代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆