Disable a textbox using CSS

前端 未结 9 1316
粉色の甜心
粉色の甜心 2020-12-24 00:23

How to disable a textbox in CSS? Currently we are having a textbox in our view which can be enabled/disabled depending on a property in the model. We are having asp.net MVC

9条回答
  •  难免孤独
    2020-12-24 00:54

    You can't disable anything with CSS, that's a functional-issue. CSS is meant for design-issues. You could give the impression of a textbox being disabled, by setting washed-out colors on it.

    To actually disable the element, you should use the disabled boolean attribute:

    
    

    Demo: http://jsfiddle.net/p6rja/

    Or, if you like, you can set this via JavaScript:

    document.forms['formName']['inputName'].disabled = true;​​​​
    

    Demo: http://jsfiddle.net/655Su/

    Keep in mind that disabled inputs won't pass their values through when you post data back to the server. If you want to hold the data, but disallow to directly edit it, you may be interested in setting it to readonly instead.

    // Similar to 
    document.forms['formName']['inputName'].readOnly = true;
    

    Demo: http://jsfiddle.net/655Su/1/

    This doesn't change the UI of the element, so you would need to do that yourself:

    input[readonly] { 
        background: #CCC; 
        color: #333; 
        border: 1px solid #666 
    }
    

    You could also target any disabled element:

    input[disabled] { /* styles */ }
    

提交回复
热议问题