html里select怎么设置和取消disabled

 我来答
百度网友96ffcf7
推荐于2017-09-07 · 知道合伙人互联网行家
百度网友96ffcf7
知道合伙人互联网行家
采纳数:22721 获赞数:118714
从事多年网络方面工作,有丰富的互联网经验。

向TA提问 私信TA
展开全部
只要元素里面设置了disabled=“xxx”不管里面为什么都为禁用。
所以原生设置:启用--dom.setAttribute("disabled","disabled") 禁用--dom.removeAttribute(“disabled”)
jquery设置:$("#id").attr("disabled","disabled")
$("#id").removeAttr("disabled")
很多人都认为设置disabled="true"是为启用,设置为“false”时为禁用,这是错的。
yt_9119
2013-10-30 · TA获得超过812个赞
知道小有建树答主
回答量:395
采纳率:100%
帮助的人:307万
展开全部

select 是支持disabled属性的,属性值为“disabled”就是禁用下拉的作用

<select disabled="disabled">
    <option value="">dfasdf</option>
    <option value="">dfasdf</option>
    <option value="">sdfasdf</option>
</select>
追问
禁止以后想让它再可用呢
追答

禁用其实就是disabled 属性去掉,这个可以用js来实现

html代码:

<select name="" id="select" disabled="disabled">
    <option value="">dfasdf</option>
    <option value="">dfasdf</option>
    <option value="">sdfasdf</option>
</select>
<button id="button">禁用/启用</button>

javascript代码

//写一个方法方便获取id
var getId = function(id){
    if(id == 'undefined'){
        return;
    }
    return document.getElementById(id);
}
var button = getId('button'),
    select = getId('select');
button.onclick = function(){
    var disabled = select.getAttribute('disabled');
    if(disabled == 'disabled'){
        //如果是禁用状态则启用,直接去掉这个属性
select.removeAttribute('disabled');
    }else{
select.setAttribute('disabled', 'disabled');
    }
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
妥协沵旳倔强
推荐于2017-09-02 · 超过13用户采纳过TA的回答
知道答主
回答量:23
采纳率:0%
帮助的人:27.9万
展开全部

使用JavaScript实现

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
    window.onload = function(){
        document.getElementById("btn").onclick = function(){
            var sel = document.getElementById("sel");
            sel.disabled = sel.disabled ? false : true;
        }
    };
</script>
<style type="text/css">

</style>
</head>
<body>
<input type="button" id="btn" value="Click"/>
<select id="sel">
<option value="">aaaaaa</option>
    <option value="">bbbbbb</option>
    <option value="">cccccc</option>
</select>
</body>
</html>
如有疑问可追问。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式