---
title: "JQuery设置select控件只读"
author: "nvry"
date: "2012-06-09T17:33:43Z"
tags: []
categories: ["Web前端"]
slug: "jquery-select"
draft: false
lastmod: "2012-06-09T17:33:43Z"
id: 1556157
url: "https://nvry.iteye.com/blog/1556157"
---

select控件是一个特别的控件，没有readOnly属性，所以要设置只读属性有点麻烦了。

方法如下：

$(document).ready(function(){  
   $("#countries").focus(function(){  
       $(this).attr('defaultIndex',$(this).attr('selectedIndex'));     
   });  
   $("#countries").change(function(){  
       $(this).attr('selectedIndex',$(this).attr('defaultIndex'));     
   });  
});

&lt;select id="countries"&gt;  
&lt;option value="1"&gt;Country1&lt;/option&gt;  
&lt;option value="2"&gt;Country2&lt;/option&gt;  
&lt;option value="3"&gt;Country3&lt;/option&gt;  
&lt;option value="4"&gt;Country4&lt;/option&gt;  
&lt;option value="5"&gt;Country5&lt;/option&gt;  
&lt;option value="6"&gt;Country6&lt;/option&gt;  
&lt;option value="7" selected="selected"&gt;Country7&lt;/option&gt;  
&lt;option value="8"&gt;Country8&lt;/option&gt;  
&lt;option value="9"&gt;Country9&lt;/option&gt;  
&lt;/select&gt;

 

后期还得优化，虽然不可再选择，但是框显示出来让人觉得还可再选择。