今天看到一大神处理下select下拉选的一段代码,在次做个笔记。这段代码主要用于禁用select标签,让其选中的值不能被改变。好了,不瞎扯了,看代码吧。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RunJS</title>
<script class="jquery library" src="/js/sandbox/jquery/jquery-1.8.2.min.js" type="text/javascript"></script>
</head>
<body>
<select id="statusprice">
<option value="0">待定价</option>
<option value="1" selected>已定价</option>
</select>
</button>
</body>
</html>
$(function(){ //方法1:参考大神的代码 $("#statusprice").focus(function(){ this.si=this.selectedIndex; }); $("#statusprice").change(function(){ this.selectedIndex=this.si; }); //方法2:比较通俗的方法,直接禁用select标签,让其至灰 $("#statusprice").attr("disabled",true); });
该代码运行后的结果是:让select一直保持选中“已定价”状态,不能选中其他选项。