javascript中input中readonly和disabled區(qū)別介紹
更新時間:2012年10月23日 21:22:00 作者:
javascript中input中readonly和disabled區(qū)別,在開發(fā)中可能會常有用到,利用js動態(tài)改變input的屬性
Readonly和Disabled是用在表單中的兩個屬性,它們都能夠做到使用戶不能夠更改表單域中的內(nèi)容。但是它們之間有著微小的差別,總結(jié)如下:
Readonly只針對input(text / password)和textarea有效,而disabled對于所有的表單元素都有效,包括select, radio, checkbox, button等。但是表單元素在使用了disabled后,當(dāng)我們將表單以POST或GET的方式提交的話,這個元素的值不會被傳遞出去,而 readonly會將該值傳遞出去(這種情況出現(xiàn)在我們將某個表單中的textarea元素設(shè)置為disabled或readonly,但是submit button卻是可以使用的)。
例子
body>
<form id="form1" name="form1" method="get" action="">
<input name="q1" type="text" id="q1" value="readonly" readonly="true" />
<input name="q2" type="text" disabled="disabled" id="q2" value="disabled" />
<input type="submit" name="Submit" value="Submit" />
</form>
js控制代碼
<body>
<form enctype="multipart/form-data" action="sign.php" method="post" name="moblie_act_form" id="moblie_act_form" >
<input type="text" class="input" id="mobile" name="mobile" value="{$mobile}" readonly="true" disabled="disabled">
<input type="button" value="修改" onClick="modify_phone()">
</form>
</html>
<script language="javascript">
function modify_phone(){
if(confirm("您確定要修改您的手機號碼嗎?")){
document.moblie_act_form.mobile.readOnly = false;
document.moblie_act_form.mobile.disabled = false;
}
return true;
}
</script>
實例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="">
<input name="q1" type="text" id="q1" value="readonly" readonly="true" />
<input name="q2" type="text" disabled="disabled" id="q2" value="disabled" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
屬性 值 描述 disabled disabled
當(dāng) input 元素首次加載時禁用此元素,這樣用戶就無法在其中寫文本,或選定它。
注釋:不能與 type="hidden" 一同使用。
readonly readonly
指示此域的值不能被修改。
注釋:僅可與 type="text" 配合使用。
總結(jié)
readonly代碼:<input type="text" name="readonly" readonly="readonly" />
readonly不可編輯,可復(fù)制,可選擇,可以接收焦點但不能被修改,后臺會接收到傳值.
disabled代碼: <input type="text" name="disabled" disabled="disabled" />
disabled不可編輯,不可復(fù)制,不可選擇,不能接收焦點,后臺也不會接收到傳值
Readonly只針對input(text / password)和textarea有效,而disabled對于所有的表單元素都有效,包括select, radio, checkbox, button等。但是表單元素在使用了disabled后,當(dāng)我們將表單以POST或GET的方式提交的話,這個元素的值不會被傳遞出去,而 readonly會將該值傳遞出去(這種情況出現(xiàn)在我們將某個表單中的textarea元素設(shè)置為disabled或readonly,但是submit button卻是可以使用的)。
例子
復(fù)制代碼 代碼如下:
body>
<form id="form1" name="form1" method="get" action="">
<input name="q1" type="text" id="q1" value="readonly" readonly="true" />
<input name="q2" type="text" disabled="disabled" id="q2" value="disabled" />
<input type="submit" name="Submit" value="Submit" />
</form>
js控制代碼
復(fù)制代碼 代碼如下:
<body>
<form enctype="multipart/form-data" action="sign.php" method="post" name="moblie_act_form" id="moblie_act_form" >
<input type="text" class="input" id="mobile" name="mobile" value="{$mobile}" readonly="true" disabled="disabled">
<input type="button" value="修改" onClick="modify_phone()">
</form>
</html>
<script language="javascript">
function modify_phone(){
if(confirm("您確定要修改您的手機號碼嗎?")){
document.moblie_act_form.mobile.readOnly = false;
document.moblie_act_form.mobile.disabled = false;
}
return true;
}
</script>
實例
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="">
<input name="q1" type="text" id="q1" value="readonly" readonly="true" />
<input name="q2" type="text" disabled="disabled" id="q2" value="disabled" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
屬性 值 描述 disabled disabled
當(dāng) input 元素首次加載時禁用此元素,這樣用戶就無法在其中寫文本,或選定它。
注釋:不能與 type="hidden" 一同使用。
readonly readonly
指示此域的值不能被修改。
注釋:僅可與 type="text" 配合使用。
總結(jié)
readonly代碼:<input type="text" name="readonly" readonly="readonly" />
readonly不可編輯,可復(fù)制,可選擇,可以接收焦點但不能被修改,后臺會接收到傳值.
disabled代碼: <input type="text" name="disabled" disabled="disabled" />
disabled不可編輯,不可復(fù)制,不可選擇,不能接收焦點,后臺也不會接收到傳值
相關(guān)文章
JS實多級聯(lián)動下拉菜單類,簡單實現(xiàn)省市區(qū)聯(lián)動菜單!
JS實多級聯(lián)動下拉菜單類,簡單實現(xiàn)省市區(qū)聯(lián)動菜單!...2007-05-05JS 無限級 Select效果實現(xiàn)代碼(json格式)
JS 無限級 Select效果實現(xiàn)代碼(json格式),學(xué)習(xí)js的朋友可以參考下。2011-08-08javascript textarea字?jǐn)?shù)限制
網(wǎng)頁中經(jīng)常會用到的一些字?jǐn)?shù)限制要求,下面的代碼應(yīng)該可以幫你解決這個問題,下面的代碼不論是text和textarea效果都是一樣的。2008-09-09