我想作一個文本框,在我jsp
成爲readonly
和它的背景色爲灰色像jQuery的disable
。以下是我的代碼:
if(a)
$('#billAccountNumber').attr('readonly', 'true');
我不喜歡使用attr('disable', 'true');
,因爲當我提交表單的值將變爲零。任何想法,而不是寫第二行代碼來改變風格?
我想作一個文本框,在我jsp
成爲readonly
和它的背景色爲灰色像jQuery的disable
。以下是我的代碼:
if(a)
$('#billAccountNumber').attr('readonly', 'true');
我不喜歡使用attr('disable', 'true');
,因爲當我提交表單的值將變爲零。任何想法,而不是寫第二行代碼來改變風格?
有2個解決方案,我知道,如果要求是不同的:
訪問此jsfiddle
in your css you can add this:
.input-disabled{background-color:#EBEBE4;border:1px solid #ABADB3;padding:2px 1px;}
in your js do something like this:
$('#test').attr('readonly', true);
$('#test').addClass('input-disabled');
希望這有助於。
另一種方法是使用隱藏的輸入字段,如某些評論所述。但請記住,在後端代碼中,您需要確保在正確的場景下驗證這個新隱藏的輸入。因此,我不推薦這種方式,因爲如果處理不當,會產生更多的錯誤。
在內部添加顏色,'.input-disabled {background-color:#EBEBE4; border:1px solid#ABADB3; padding:2px 1px; color:rgb(84,84,84);}',perfect。 –
爲什麼你不把帳號放在div中。按照您的喜好來設計樣式,然後在包含帳號的表單中輸入一個隱藏的輸入。然後,當表單被提交時,值應該通過而不是null。
可以像下面那樣添加禁用功能,並可以獲取提交數據。這樣的事情.. DEMO
的Html
<input type="hidden" name="email" value="email" />
<input type="text" id="dis" class="disable" value="email" name="email" >
JS
$("#dis").attr('disabled','disabled');
CSS
.disable { opacity : .35; background-color:lightgray; border:1px solid gray;}
按照你的問題,這是你可以做什麼
HTML
<textarea id='sample'>Area adskds;das;dsald da'adslda'daladhkdslasdljads</textarea>
JS/jQuery的
$(function() {
$('#sample').attr('readonly', 'true'); // mark it as read only
$('#sample').css('background-color' , '#DEDEDE'); // change the background color
});
或添加類在您需要的造型
$('#sample').addClass('yourclass');
CSS
讓
如果有人正在考慮第二個選擇:我能夠添加一個類來改變背景顏色,但事件有關該類不會爲具有新類的對象觸發。 –
如果你的瀏覽器不支持,你可以使用CSS3 :read-only
selector:
input[type="text"]:read-only {
cursor: normal;
background-color: #f8f8f8;
color: #999;
}
是否有可能爲你創造殘疾人領域的變量,一旦你提交你可以使用這個變量,而不是拉出來的。這樣你不應該得到一個NULL值。 – Ljubisa
這是一個好主意,但我需要在後端代碼中進行很多更改,因此,我寧願使用相同的變量。 –
也許'disabled = true'? – Cilan