Customizing jQuery selectmenu dropdown button on after item selected?

拈花ヽ惹草 提交于 2019-12-13 20:50:08

问题


I'm looking to create a dropdown for colors that has each color as a small square next to the item.

Have:

Want (roughly):

This version (jsfiddle) works fine for the dropdown items themselves, but I'd like the button to also update to have the colored square next to it.

$.widget("custom.coloriconselectmenu", $.ui.selectmenu, {  
  _renderItem: function (ul, item) {  
    var li = $("<li>", {text: item.label});  
    var bgColorStyle = 'background-color:' + item.element.attr("data-color");  
    var fullStyle = "float: left; width: 21px; height: 19px; margin-right: 7px;" + bgColorStyle;  
    $("<div>", {style: fullStyle}).appendTo(li);  
    return li.appendTo(ul);  
  }
});  

$("#selectId").coloriconselectmenu({icons: {button: "custom-button"}});

Is there a good way to modify the dropdown button on value update to include the color square like in the dropdown?


回答1:


Modify your code as follows so that it responds to the change event:

$( "#selectId" ).coloriconselectmenu({ 
    icons: { button: "custom-button" }, 
    change: function(event, ui){ 
        var selectedColor = $(this).find("option:selected").attr("data-color");
        //alert(selectedColor);
        var hiddenSelectMenuBtn = "#" + $(this).attr("id")+"-button .ui-selectmenu-text"
        //alert($(hiddenSelectMenuBtn).text());

        var fullStyle = "float: left; width: 21px; height: 19px; margin-right: 7px; background-color:" + selectedColor;  
        $("<span>", {style: fullStyle}).prependTo($(hiddenSelectMenuBtn));  
}); 

You need to select the "select menu text", which isn't readily exposed via a function or property.

You can see this in action in the updated jsfiddle.



来源:https://stackoverflow.com/questions/29528317/customizing-jquery-selectmenu-dropdown-button-on-after-item-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!