【问题标题】:give styles to options text in select box using jquery使用 jquery 为选择框中的选项文本提供样式
【发布时间】:2018-03-04 13:04:44
【问题描述】:

我在选择框中有一个选项。我需要设置特定文本的样式,为选项提供文本证明。我正在使用 php、bootstrap 和 jquery。

这是我的代码:

 <option style="margin:5px 5px;"value="<?php echo $totvalue['ID']; ?>" data-position="<?php echo $i;?>"><?php                 
                    echo $totvalue['Abbreviation'].' - '.$quotation.' - '.$totvalue['Name'].' - '.$totvalue['Category']; ?></option>

这是

<option style="margin:5px 5px;" value=1002>TAKAJ - A - SAMSUUNG - 4G</option>
<option style="margin:5px 5px;" value=1003>YEG - L - SONY - 3G</option>

我需要- A -- L - 在选项中才能进入&lt;span class="label label-success"&gt;。如何使用 jquery 实现这一点..

【问题讨论】:

  • 我不认为你可以设置选项样式:jsfiddle.net/bu9pmL21 找一些像 select2 这样的插件
  • option 元素仅是纯文本。您将需要使用自定义选择插件。
  • 是的,我知道,但它需要。任何其他解决方案
  • @BenM 你能给插件起个名字吗

标签: php jquery html css twitter-bootstrap


【解决方案1】:

选择输入中的选项由浏览器生成。你不能在那里添加样式。 options in bootstrap

但是你可以像这里一样在 jQuery 中创建自己的选择:

// Iterate over each select element
$('select').each(function () {

    // Cache the number of options
    var $this = $(this),
        numberOfOptions = $(this).children('option').length;

    // Hides the select element
    $this.addClass('s-hidden');

    // Wrap the select element in a div
    $this.wrap('<div class="select"></div>');

    // Insert a styled div to sit over the top of the hidden select element
    $this.after('<div class="styledSelect"></div>');

    // Cache the styled div
    var $styledSelect = $this.next('div.styledSelect');

    // Show the first select option in the styled div
    $styledSelect.text($this.children('option').eq(0).text());

    // Insert an unordered list after the styled div and also cache the list
    var $list = $('<ul />', {
        'class': 'options'
    }).insertAfter($styledSelect);

    // Insert a list item into the unordered list for each select option
    for (var i = 0; i < numberOfOptions; i++) {
        $('<li />', {
            text: $this.children('option').eq(i).text(),
            rel: $this.children('option').eq(i).val()
        }).appendTo($list);
    }

    // Cache the list items
    var $listItems = $list.children('li');

    // Show the unordered list when the styled div is clicked (also hides it if the div is clicked again)
    $styledSelect.click(function (e) {
        e.stopPropagation();
        $('div.styledSelect.active').each(function () {
            $(this).removeClass('active').next('ul.options').hide();
        });
        $(this).toggleClass('active').next('ul.options').toggle();
    });

    // Hides the unordered list when a list item is clicked and updates the styled div to show the selected list item
    // Updates the select element to have the value of the equivalent option
    $listItems.click(function (e) {
        e.stopPropagation();
        $styledSelect.text($(this).text()).removeClass('active');
        $this.val($(this).attr('rel'));
        $list.hide();
        /* alert($this.val()); Uncomment this for demonstration! */
    });

    // Hides the unordered list when clicking outside of it
    $(document).click(function () {
        $styledSelect.removeClass('active');
        $list.hide();
    });

});

See on jsFiddle

【讨论】:

  • 我正在使用多选插件。我已经尝试了上述方法但没有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 2022-01-12
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多