日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

html標簽的屬性——disabled與readonly區別

作者:xinlianluohan 更新時間: 2023-12-13 編程語言

一、介紹readonly和disabled作用

readonly和disabled 兩種屬性的寫法,如

<input type="text" name="name"value="xxx” readonly="true"/>
<input type="text"name="name" value="xxx" disabled="true"/>

相同點:

如果設為true。則form屬性不能被編輯、文本框不能輸入文字。

?不同點:

1、樣式不同。
? ? ? ? readonly只是使文本框不能輸入,外觀沒有變化。
? ? ? ? disabled會使文本框變灰。


2、有效范圍不同。
? ? ? ? readonly只針對input(text / password)和textarea之類可以輸入文本的輸入項有效disabled對于所有的表單元素都有效


3、是否能獲取鼠標焦點。
readonly設為true,用戶不能編輯,但鼠標仍然可以聚焦。disabled設為true,輸入項不能獲取焦點,用戶的所有操作(標點擊、鍵盤輸入等)對該輸入項都無效


4、是否回傳數據。
表單元素使用了readonly后,會將該值傳遞出去,即通過request.getParameter("name"可以得到文本框內的內容,而表單元素使用了disabled后,當我們將表單以POST或GET的方式提交的話,這個元素的值不會被傳遞出去,即通過request.getParameter(name”)得不到文本框中的內容 (如果有的話)。

二、jquery設置disabled屬性與移除disabled屬性

//兩種方法設置disabled屬性

$('#button').attr("disabled",true);

$('#button').attr("disabled","disabled");
//一般來說我比較推薦上面的這種來設置

//三種方法移除disabled屬性

$('#button').attr("disabled",false);

$('#button').removeAttr("disabled");
//一般來說我比較推薦上面的這種來移除

$('#button').attr("disabled","");

三、當要提交表單時,如果需要disable的屬性值,可以再提交前移除掉disable屬性

  • 移除disabled

批量移除含有disabled屬性

 //提交前移除disabled
$('.tbody').find('input, textarea, button,select').removeAttr("disabled");
  • readonly
 /*readonly unselectable="on" 該屬性跟disable類似,
    input 元素,不可編輯,不可復制,不可選擇,不能接收焦點,
    設置后文字的顏色也會變成灰色,但是后臺可以接收到傳值。
*/
  示例:
  <input type="text"  readonly  unselectable="on" >
  • select下拉框:
    //加入如下的樣式
    <style>
        select[readonly] {
            background: #eee;
            cursor: no-drop;
        }
        select[readonly] option {
            display: none;
        }
    </style>
    //使用時,加上readonly屬性就行, 但由于有上面的樣式在,所以下拉框是點不動
    <select name="" id="" readonly>
        <option value="">1</option>
        <option value="">1</option>
    </select>
  • radio單選框:
  •  //input框很簡單直接在點擊時加上return false,來阻止點擊事件
     <input type="radio" onclick="return false;">蘋果
     <input type="radio" onclick="return false;">榴蓮
    

原文鏈接:https://gaodengwen.blog.csdn.net/article/details/134635552

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新