其实,既然不能修改,就不应该用select
当然这里只是说明一个问题而已.
关键的是要花时间,对于基本控件的属性分析清楚.
Code
项目中遇到一个问题:不允许修改控件的值;为了不与原来的页面冲突,还需要在用户页面显示这几个控件;尽量减少改动;
碰到的一个例子是:struts html:select标签,在用户点击修改时,表的主键不允许修改,但又需要显示给用户;
开始用readonly,发现select就不起作用了;之后改成disabled,但表单不给server传值了(发现这个设计不太好吧,按理应该传给server,server用不用是他自己的事情);后来求助js,google一下,找了些思路,自己总结了一下,目前算是可以解决。
text标签可以用readonly
下面用select举例子:
1. disabled
对于不给server传值的问题,我们可以用一个隐藏字段hidden。
同时我还发现hidden具有优先权:即两个字段一样,hidden值被优先选择。
2. JavaScript:onclick方法
发现这个方法有意思:点击下拉,就弹出alert,之后下拉列表就收回,这样用户就不能修改了。但随后测试发现,可以用键盘来改动(tab到下拉列表,之后用上下箭头来改动)
3. JavaScript:onfocus+onchange
其实摸索过程还有琐屑:比如试过onkeydown等,总是有各种问题。
最后选择方法:onfocus保存字段值,onchange提示用户不允许修改+还原值。
<SCRIPT language=JavaScript>
var temp;
function onFocu(obj){
//alert("onfocus");
temp = obj.value;
}
function onChan(obj){
alert("主键不允许修改");
obj.value = temp;
}
</SCRIPT>
各位如果有更好的方法,记得留言。
交流使人进步!