【问题标题】:Add selected attribute to option in select menu with jQuery使用 jQuery 将选定属性添加到选择菜单中的选项
【发布时间】:2011-08-13 00:53:35
【问题描述】:

我正在制作一个选择菜单插件来替换丑陋的默认选择并在不同的操作系统中保持一致。

这是演示(仅限 firefox 和 webkit)
http://spacirdesigns.com/selectMenu/

它已经在工作了,但是我在将“selected”属性分配给选项时遇到了问题。该代码适用于任何其他属性,但我无法使其适用于所选属性。

这行得通:

select.find('option')
    .removeAttr('whatever')
    .eq(index).attr('whatever', 'hello');

这不是:

select.find('option')
    .removeAttr('selected')
    .eq(index).attr('selected', 'selected');

这是目前为止的代码:

(function($){

        $.fn.selectMenu = function() {

            var select = this;
            select.hide();

            var title = select.attr('title');
            var arrow = 'img/arrow.png';
            var items = '';

            select
                .children('option')
                .each(function(){
                    var item = $(this).text();
                    if ($(this).val() != '') { 
                        $(this).attr('value', item);
                    }
                    items += '<li>' + item + '</li>'
                });

            var menuHtml =
                '<ul class="selectMenu">' + 
                '<img src="' + arrow + '" alt=""/>' +
                '<li>' + title + '</li>' +
                '<ul>' + items  + '</ul>' +
                '</ul>';

            select.after(menuHtml);

            var menu = $(this).next('ul');
            var list = menu.find('ul');

            menu
                .hover(function(){}, function(){
                    list.hide();
                })
                .children('li').hover(function(){
                    list.show();
                });

            menu.find('ul li').click(function(){
                var index = $(this).index();
                menu.children('li').text($(this).text());
                select.find('option')
                    .removeAttr('selected')
                    .eq(index).attr('selected', 'selected');
                list.hide();
            });

        };

    })(jQuery);

【问题讨论】:

  • 您是否尝试过使用.attr('selected', true)?
  • .attr('selected', true) 也不起作用。
  • 我在 FF 4 的测试页面上尝试了这段代码:对我来说很好!属性不是直接在 FireBug 中分配的,但是每次我更改索引属性时都会更改 select 的值和选定选项。
  • 是的,我刚刚添加了一个按钮来提醒选定的状态,它可以正常工作,只是在萤火虫中没有显示。那让我发疯了。查看演示spacirdesigns.com/selectMenu

标签: jquery menu option selected


【解决方案1】:

考虑到您的最新评论,我认为您的问题是萤火虫,而不是您的代码。为什么这适用于除“selected”之外的其他属性,因为从下拉列表中选择一个选项不会修改 DOM 中的 selected 属性。我向你保证,你的代码没有问题。

【讨论】:

    【解决方案2】:

    查看this previous detailled answer on SO:

    如果你真的想用 selected 属性维护 HTML 输出,而不仅仅是让 jQuery 在 select 元素上维护正确的 selectedIndex 属性,你可以使用原始的 settAttr() 函数来破解:

    select[0].options[select[0].selectedIndex].setAttribute('selected','selected');
    

    但是,只要你继续使用 jQuery 方法来处理 val() 或 ':selected',你应该不会遇到任何问题,只有在解析 HTML 以查找选定属性时才会遇到问题,这是你不应该的做,永远不要。

    【讨论】:

    【解决方案3】:

    从 jQuery 1.6 开始,“要检索和更改 DOM 属性,例如表单元素的选中、选择或禁用状态,请使用 .prop() 方法。”

    $("#someselect option[value=somevalue]").prop("selected", "selected")
    

    编辑:

    选择选项:

    $("#someselect option[value=somevalue]").prop("selected", true)
    

    取消选择选项:

    $("#someselect option[value=somevalue]").prop("selected", false)
    

    【讨论】:

    • 这应该是最好的答案,比其他的更简单,更老的答案
    • 谢谢@sidarcy。这对我帮助很大。当然它应该排在最前面。
    • 不应该是.prop("selected", true)吗?我记得,在这种情况下使用旧方法时,它必须是.attr("selected", "selected")。
    猜你喜欢
    • 2015-03-25
    • 1970-01-01
    • 2010-09-23
    • 2015-08-30
    • 2014-11-21
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多