HTML form readonly SELECT tag/input

前端 未结 30 2352
陌清茗
陌清茗 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 09:12

    A bit late to the party. But this seems to work flawlessly for me

    select[readonly] {
        pointer-events:none;
    }
    
    0 讨论(0)
  • 2020-11-22 09:13

    This is the simplest and best solution. You will set a readolny attr on your select, or anyother attr like data-readonly, and do the following

    $("select[readonly]").live("focus mousedown mouseup click",function(e){
        e.preventDefault();
        e.stopPropagation();
    });
    
    0 讨论(0)
  • 2020-11-22 09:15

    Simply, remove the disabled attribute before submit the form.

        $('form').submit(function () {
            $("#Id_Unidade").attr("disabled", false);
        });
    
    0 讨论(0)
  • 2020-11-22 09:16

    If you disable a form field, this won't be sent when form is submitted. So if you need a readonly that works like disabled but sending values do this :

    After any change in readonly properties of an element.

    $('select.readonly option:not(:selected)').attr('disabled',true);
    
    $('select:not([readonly]) option').removeAttr('disabled');
    
    0 讨论(0)
  • 2020-11-22 09:16

    Rather than the select itself, you could disable all of the options except for the currently selected option. This gives the appearance of a working drop-down, but only the option you want passed in is a valid selection.

    0 讨论(0)
  • 2020-11-22 09:17

    We could also use this

    Disable all except the selected option:

    <select>
        <option disabled>1</option>
        <option selected>2</option>
        <option disabled>3</option>
    </select>
    

    This way the dropdown still works (and submits its value) but the user can not select another value.

    Demo

    0 讨论(0)
提交回复
热议问题