在jquery中将文本框设置为只读,将背景颜色设置为灰色 [英] Set textbox to readonly and background color to grey in jquery

查看:1030
本文介绍了在jquery中将文本框设置为只读,将背景颜色设置为灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我想在我的 jsp 中创建一个文本框,成为 readonly 及其背景颜色为灰色,如Jquery中的 disable 。以下是我的代码:

I would like to make a text box in my jsp to become readonly and its background color to grey like disable in Jquery. The following is my code :

if(a)
  $('#billAccountNumber').attr('readonly', 'true');

我不喜欢使用 attr('disable','true'); ,因为当我提交表单时,该值将变为null。任何想法而不是写第二行代码来改变风格?

I not prefer using attr('disable', 'true');, because the value will become null when I submit form. Any ideas instead of write second line of code to change the style?

推荐答案

有两种解决方案:

访问此 jsfiddle

in your css you can add this:
     .input-disabled{background-color:#EBEBE4;border:1px solid #ABADB3;padding:2px 1px;}

in your js do something like this:
     $('#test').attr('readonly', true);
     $('#test').addClass('input-disabled');

希望这个帮助。

另一种方式正在使用一些评论中提到的隐藏输入字段。但请记住,在后端代码中,您需要确保在正确的方案中验证此新隐藏的输入。因此我不推荐这种方式,因为如果它处理不当会产生更多错误。

Another way is using hidden input field as mentioned by some of the comments. However bear in mind that, in the backend code, you need to make sure you validate this newly hidden input at correct scenario. Hence I'm not recommend this way as it will create more bugs if its not handle properly.

这篇关于在jquery中将文本框设置为只读,将背景颜色设置为灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆