技海泛舟(个人技术研究)

  • 首页
  • 日积月累
  • 学习计划
  • 随想
  • project
  • 关于
技海泛舟
一个技术宅的博客
  1. 首页
  2. 日积月累
  3. JavaScript
  4. 正文

js动态添加和移除disabled属性和style

2021年12月3日 2174点热度

js动态添加和移除disabled属性

js操作:

// mode 1
function disableElement(element,val){
    document.getElementById(element).disabled=val;
}

// mode 2
document.getElementById("element").disabled="";

jQuery进行操作:

//两种设置disabled属性
$('#uid').attr("disabled",true);
$('#uid').attr("disabled","disabled");
//三种移除disabled属性
$('#uid').attr("disabled",false);
$('#uid').removeAttr("disabled");
$('#uid').attr("disabled","");

获取s:textfield,并设置其disabled属性:

function disableTextfieldofAccountDiv(element,val) {
    $(element).find(":textfield").attr('disabled',val);
}

js动态显示和隐藏style

function changefixe(frm,str){
    if(str=="0"){
        document.getElementById("uid").style.display="block";//显示
        document.getElementById("uid").disabled="true";//残废disabled
    }else{
        document.getElementById("uid").style.display="none";//隐藏
        document.getElementById("uid").disabled="";//去除disabled
    }
}

注: 和disabled具有相同功能的还有readOnly,

区别是:

  • Readonly(只读属性): 只针对input(text/ password)和textarea、radio、checkbox有效。

  • disabled(是某个表单元素参数): 对于所有的表单元素都有效,包括select,radio, checkbox, button等。

但是表单元素在使用了disabled后,当我们将表单以POST或GET的方式提交的话,这个元素的值不会被传递出去,而readonly会将该值传递出去(这种情况出现在我们将某个表单中的textarea元素设置为disabled或readonly,但是submitbutton却是可以使用的)。

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2021年12月6日
< 上一篇
下一篇 >
归档
  • 2024 年 11 月
  • 2024 年 8 月
  • 2024 年 5 月
  • 2024 年 2 月
  • 2023 年 12 月
  • 2023 年 11 月
  • 2023 年 9 月
  • 2023 年 6 月
  • 2022 年 12 月
  • 2022 年 11 月
  • 2022 年 10 月
  • 2022 年 9 月
  • 2022 年 8 月
  • 2022 年 7 月
  • 2022 年 6 月
  • 2022 年 5 月
  • 2022 年 4 月
  • 2022 年 3 月
  • 2022 年 2 月
  • 2022 年 1 月
  • 2021 年 12 月
  • 2021 年 11 月
  • 2021 年 10 月
  • 2021 年 5 月
分类
  • Android
  • Arduino
  • cordova
  • css
  • go
  • html5
  • JavaScript
  • nodejs
  • oracle
  • project
  • system
  • uni-app
  • vscode
  • vue
  • 学习计划
  • 摘抄
  • 随想

COPYRIGHT © 技海泛舟(个人技术研究). 2021-2023. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蜀ICP备2026065271号-1

蜀公网安备XXXXX号