HTML form readonly SELECT tag/input

前端 未结 30 2389
陌清茗
陌清茗 2020-11-22 08:28

According to HTML specs, the select tag in HTML doesn\'t have a readonly attribute, only a disabled attribute. So if you want to keep

30条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 08:55

    In addition to disabling the options that should not be selectable i wanted to actually make them dissapear from the list, but still be able to enable them should i need to later:

    $("select[readonly]").find("option:not(:selected)").hide().attr("disabled",true);
    

    This finds all select elements with a readonly attribute, then finds all options inside those selects that are not selected, then it hides them and disables them.

    It is important to separate the jquery query in 2 for performance reasons, because jquery reads them from right to left, the code:

    $("select[readonly] option:not(:selected)")
    

    will first find all unselected options in the document and then filter those that are inside selects with a readonly attribute.

提交回复
热议问题