简体   繁体   English

新添加的选择选项不显示标题属性

[英]Newly appended select options not displaying title attribute

I'm currently using ajax to append new options to a multiple select box, but even though I'm trying to add title attributes to them, they don't seem to be displaying at all. 我目前正在使用ajax将新选项附加到多选框,但是即使我试图向它们添加标题属性,它们似乎也没有显示。 Is there something that I'm missing? 有什么我想念的吗?

在此处输入图片说明

This is done in Coffeescript: 这是在Coffeescript中完成的:

$.ajax(
        type: 'get'
        url: '/Filter/LookupClassification'
        data: ( term: inputVal )
        dataType: 'json'
        success: (response)->
            select = document.getElementById('getClassBox')
            select.options.length = 0

            $.each(response, (key, value)->
                option = $(
                    '<option/>'
                    'title': value.toUpperCase()
                    'value': key
                ).text(key + ' - ' + value.toUpperCase())

                $('#getClassBox').append(option)
            )

            $('#selectClassBox option').each((index, value)->
                val1 = $(value).val()
                if $('#getClassBox option').val() is val1
                    $('#getClassBox option[value=' + val1 + ']').remove()
            )
    )

The <option> element can't have a "title" attribute. <option>元素不能具有“ title”属性。 See the spec. 参见规格。

edit — by that I mean that while it's fine to add a "title" attribute if you want to, the browser won't pay attention to it like it would to a "title" attribute on a <div> or <button> element. 编辑 -我的意思是,虽然可以根据需要添加“ title”属性,但浏览器不会像<div><button>元素上的“ title”属性那样关注它。

edit again — also you should probably ignore this since, though it's not in the HTML5 spec, the "title" attribute on <option> elements is apparently supported in some browsers. 再次编辑 -您可能也应该忽略它,因为尽管它不在HTML5规范中,但在某些浏览器中显然支持<option>元素上的“ title”属性。

You could simply write this to add options to your dropdown: 您只需编写以下代码即可为下拉菜单添加选项:

select.options[select.options.length] = new Option(key + ' - ' + value.toUpperCase(), key);

Don't bother about title attributes. 不要为标题属性而烦恼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM