HTML select shows value instead of text

后端 未结 5 1022
花落未央
花落未央 2021-01-11 16:53

Is it possible in a html select, to display the value of the option instead of the text?

It\'s gonna be used for displaying a long disription in the dro

5条回答
  •  时光取名叫无心
    2021-01-11 17:11

    This is the solution.

    $("select option").each(function () {
        $(this).attr("data-label", $(this).text());
    });
    $("select").on("focus", function () {
        $(this).find("option").each(function () {
            $(this).text($(this).attr("data-label"));
        });
    }).on("change mouseleave", function () {
        $(this).focus();
        $(this).find("option:selected").text($(this).val());
        $(this).blur();
    }).change();
    

提交回复
热议问题