简体   繁体   中英

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. Is there something that I'm missing?

在此处输入图片说明

This is done in 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. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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