How to make a dropdown readonly using jquery?

后端 未结 21 1037
终归单人心
终归单人心 2020-12-02 06:54

I am using the following statement to make it readonly but it is not working.

$(\'#cf_1268591\').attr(\"readonly\", \"readonly\"); 

I don\

相关标签:
21条回答
  • 2020-12-02 07:23

    I'd make the field disabled. Then, when the form submits, make it not disabled. In my opinion, this is easier than having to deal with hidden fields.

    //disable the field
    $("#myFieldID").prop( "disabled", true );           
    
    //right before the form submits, we re-enable the fields, to make them submit.
    $( "#myFormID" ).submit(function( event ) {
        $("#myFieldID").prop( "disabled", false );
    });     
    
    0 讨论(0)
  • 2020-12-02 07:23

    Maybe you can try this way

    function myFunction()
    {
       $("select[id^=myID]").attr("disabled", true);
       var txtSelect = $("select[id^=myID] option[selected]").text();
    }
    

    This sets the first value of the drop-down as the default and it seems readonly

    0 讨论(0)
  • 2020-12-02 07:23

    html5 supporting :

    `
         $("#cCity").attr("disabled", "disabled"); 
         $("#cCity").addClass('not-allow');
    

    `

    0 讨论(0)
  • 2020-12-02 07:25

    Here is a slight variation on the other answers that suggest using disabled. Since the "disabled" attribute can actually have any value and still disable, you can set it to readonly, like disabled="readonly". This will disable the control as usual, and will also allow you to easily style it differently than regular disabled controls, with CSS like:

    select[disabled=readonly] {
        .... styles you like for read-only
    }
    

    If you want data to be included submit, use hidden fields or enable before submit, as detailed in the other answers.

    0 讨论(0)
  • 2020-12-02 07:29

    This code will first store the original selection on each dropdown. Then if the user changes the selection it will reset the dropdown to its original selection.

    http://jsfiddle.net/4aHcD/22/

    //store the original selection
    $("select :selected").each(function(){
        $(this).parent().data("default", this);
    });
    
    //change the selction back to the original
    $("select").change(function(e) {
        $($(this).data("default")).prop("selected", true);
    });
    
    0 讨论(0)
  • 2020-12-02 07:31
    $('#cf_1268591').attr("disabled", true); 
    

    drop down is always read only . what you can do is make it disabled

    if you work with a form , the disabled fields does not submit , so use a hidden field to store disabled dropdown value

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