【问题标题】:How to add title attribute to Chosen option tags?如何将标题属性添加到选择的选项标签?
【发布时间】:2013-01-28 19:45:16
【问题描述】:

我正在使用“Chosen plugin”,但不确定如何将属性添加到选择列表的选项标签中。

我已经在准备好的文档上使用 jQuery 进行了尝试,但没有运气。

【问题讨论】:

  • 很可能你不能......但你能更好地解释你在说什么吗?
  • 你找到这个选项了吗? Chosen 似乎正确地从底层 Option 对象复制了 classNames,但忽略了 title 属性。
  • @NicholasTolleyCottrell 你还需要答案吗?我注意到你不喜欢这个问题,所以你不会收到新答案的通知

标签: jquery-chosen


【解决方案1】:

您需要将 Chosen.Jquery.js 修改为以下内容才能正常工作

使用选择的 1.0 版本

第62行添加这行代码

this.parsed.push({
        array_index: this.parsed.length,
        options_index: this.options_index,
        value: option.value,
        text: option.text,
        html: option.innerHTML,
    title: option.title, // this is Line 62 add this line
        selected: option.selected,
        disabled: group_disabled === true ? group_disabled : option.disabled,
        group_array_index: group_position,
        classes: option.className,
        style: option.style.cssText
      });

修改第 255 行

return "<li title=\"" + option.title +"\"  class=\"" + (classes.join(' ')) + "\"" + style + " data-option-array-index=\"" + option.array_index + "\">" + option.search_text + "</li>";

【讨论】:

    【解决方案2】:

    I have tried this..but no luck

    这取决于你尝试了什么。 试试这个函数,用你希望它选择的对应物继承标题的选择元素调用它

    function cloneTitles(selectBox) {
        //make sure it has already been chosen-ised
        selectBox = $(selectBox).chosen();
    
        //get all the original options, should be in same order as chosen ones
        var origOpts = selectBox.find('option');
    
        //get all the chosen-created 'options'
        //NB there may be a better way to grab the chosen created element
        var chznOpts = selectBox.next().find('.active-result')
    
        //foreach option
        origOpts.each(function(index, origOpt) {
            //copy the attribute from the original
            $(chznOpts[index]).attr('title', $(origOpt).attr('title'));
        });
    }
    

    I have tried this on document ready
    您的问题可能是您在 selected.js 转换您的选择框之前所做的任何尝试,但此功能应该可以缓解这种情况。

    另外,如果您需要多个选择框,只需使用.each()
    (例如selectArray.each(function(i, select) { cloneTitles(select); })

    我假设您使用的是 jQuery 而不是 Prototype(鉴于您的最后一行)
    因此,如果您愿意,可以传入 '#id' 而不是
    可以修改代码以克隆给定属性,而不是 'title',甚至是给定属性的数组!

    【讨论】:

    • 根据@Nicholas Tolley Cottrell 的评论,假设您的选择元素的选项已经设置了标题属性。但是,如果没有,没有恐惧,修改函数接受一系列标题字符串并使用那些而不是'chznopts'注意,只要选择更新下拉目,通常只会被.trigger('liszt:updated')所以你可能是ran不用担心这个
    • 大家好,我只是想说声谢谢!
    【解决方案3】:

    使用选择的 1.1 版本

    html: option.innerHTML,   // Search
    title: option.title,      // Add
    

    .

    option_el.innerHTML = option.search_text;    // Search
    option_el.title = option.title;              // Add
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 2014-12-30
      • 1970-01-01
      • 2013-12-12
      相关资源
      最近更新 更多